What will be the output of the following Python code? L = [1, 2, 1, 5, 4, 1,…

2026

What will be the output of the following Python code?

L = [1, 2, 1, 5, 4, 1, 3, 1]
print(L.index(L.count(1)))

  1. A.

    5

  2. B.

    4

  3. C.

    ValueError

  4. D.

    IndexError

Attempted by 118 students.

Show answer & explanation

Correct answer: B

First, L.count(1) counts the occurrences of 1 in the list, resulting in 4. Next, L.index(4) searches for the first index of value 4 within the list. Since the element at index 4 is indeed 4, the function returns 4.

Explore the full course: Rssb Senior Computer Instructor