Identify the correct Output of the following python code : L=[3,3,2,2,1,1]…
2023
Identify the correct Output of the following python code : L=[3,3,2,2,1,1] L.append(L.pop(L.pop())) print(L)
- A.
[3, 2, 2, 1, 3] / [3, 2, 2, 1, 3]
- B.
[3, 3, 2, 2, 3] / [3, 3, 2, 2, 3]
- C.
[1, 3, 2, 2, 1] / [1, 3, 2, 2, 1]
- D.
[1, 1, 2, 2, 3, 3] / [1, 1, 2, 2, 3, 3]
Attempted by 1527 students.
Show answer & explanation
Correct answer: A
Step-by-step evaluation:
Start with L = [3, 3, 2, 2, 1, 1].
Evaluate the inner call L.pop() (no index). This removes and returns the last element 1. Now L = [3, 3, 2, 2, 1].
Next evaluate L.pop(1). This removes and returns the element at index 1 (the second element), which is 3. Now L = [3, 2, 2, 1].
Now perform L.append(3) with the value returned from the previous pop. After appending, L = [3, 2, 2, 1, 3].
Printed output: [3, 2, 2, 1, 3]
Note: The inner pop() is evaluated before the outer pop(index), and each pop mutates the list. That evaluation order and mutation are why the final list is as shown.