Predict the output
What does this print?
path, out = [], []
for a in [1, 2]:
path.append(a)
out.append(path)
path.pop()
print(out)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A [[], []]
Right: both entries are the same list object, which is empty after the pops.
Recording path[:] stores a snapshot, which is the one-character difference between right and wrong answers here.
Why not the others?
- B [[1], [2]]. No copy was taken, so the entries share one list.
- C [1, 2]. out holds lists, not numbers.