nerdexam
Python_Institute

PCEP-30-02 · Question #29

What is the output of the following snippet? ``python my_list_1 = [1, 2, 3] my_list_2 = [] for v in my_list_1: my_list_2.insert(0, v) print(my_list_2) ``

The correct answer is B. [3, 2, 1]. list.insert(0, v) always places each new value at index 0 (the front), pushing everything else right. Iterating [1, 2, 3] in order gives: [1] → [2, 1] → [3, 2, 1], so B is correct. A [1, 2, 3] is wrong - that's what you'd get using .append(v) (adds to the end), not .insert(0…

Question

What is the output of the following snippet?
my_list_1 = [1, 2, 3]
my_list_2 = []
for v in my_list_1:
 my_list_2.insert(0, v)
print(my_list_2)

Options

  • A[1, 2, 3]
  • B[3, 2, 1]
  • C[3, 2, 1]
  • D[1, 1, 1]

How the community answered

(25 responses)
  • A
    4% (1)
  • B
    76% (19)
  • C
    12% (3)
  • D
    8% (2)

Explanation

list.insert(0, v) always places each new value at index 0 (the front), pushing everything else right. Iterating [1, 2, 3] in order gives: [1][2, 1][3, 2, 1], so B is correct.

  • A [1, 2, 3] is wrong - that's what you'd get using .append(v) (adds to the end), not .insert(0, v).
  • C [3, 2, 1] is a deliberate duplicate of B included as a trap; since B appears first and is the labeled correct answer, C is the distractor.
  • D [1, 1, 1] is wrong - nothing in the code repeats a single value; each element of my_list_1 is inserted exactly once.

Memory tip: Think of .insert(0, v) as "cutting to the front of the line" - the last person to arrive ends up first, so the final list is the reverse of the original.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice