Practice mode Python

What is the output of the following Python code? print(list(zip(“ab”, )))

Correct answer B. [('a', 1), ('b', 2)]

Explanation

By default zip() stops as soon as the shortest input is exhausted, so the extra 3 is silently ignored and no padding with None happens. To pad shorter inputs use itertools.zip_longest, and to raise an error on unequal lengths pass strict=True, available since Python 3.10.

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.