What is the output of the following Python code? a = ; a.append(); b = ; b.extend(); print(len(a), len(b))
Correct answer B. 3 4
Explanation
append() adds its argument as a single item, so a becomes [1, 2, [3, 4]] with length 3. extend() iterates over its argument and adds each element, so b becomes [1, 2, 3, 4] with length 4. Confusing the two is a frequent source of unexpected nested lists.
Answer checked How we write and check questions
Discussion