PCEP-30-02 · Question #26
The second assignment: ``python 1 vals = [0, 1, 2] 2 vals[0], vals[1] = vals[1], vals[2] ``
The correct answer is B. doesn't change the list's length. Option B is correct because this line performs simultaneous index assignment - it reassigns existing slots in the list without adding or removing any elements. The right-hand side vals[1], vals[2] is evaluated first as a tuple (1, 2), then unpacked into vals[0] and vals[1]…
Question
1 vals = [0, 1, 2]
2 vals[0], vals[1] = vals[1], vals[2]
Options
- Aextends the list
- Bdoesn't change the list's length
- Cshortens the list
How the community answered
(68 responses)- A12% (8)
- B82% (56)
- C6% (4)
Explanation
Option B is correct because this line performs simultaneous index assignment - it reassigns existing slots in the list without adding or removing any elements. The right-hand side vals[1], vals[2] is evaluated first as a tuple (1, 2), then unpacked into vals[0] and vals[1], leaving the list as [1, 2, 2] - still three elements.
Option A (extends the list) is wrong because extending requires operations like .append(), .extend(), or += [...]; assigning to an existing index cannot create a new slot.
Option C (shortens the list) is wrong because removing elements requires .pop(), .remove(), del, or slicing reassignment - none of which appear here.
Memory tip: Think of list indices as labeled mailboxes - you can swap what's inside the boxes, but index assignment alone can never add or remove a mailbox.
Community Discussion
No community discussion yet for this question.