Practice mode Python

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.