ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
it = iter([1, 2, 3, 4, 5])
pairs = list(zip(it, it))
print(pairs, list(it))
  1. A. [(1, 2), (3, 4)] []
  2. B. [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)] []
  3. C. [(1, 2), (3, 4)] [5]
  4. D. [(1, 2), (3, 4), (5, None)] []

Correct answer: A. [(1, 2), (3, 4)] []

Both arguments are the SAME iterator, so zip takes alternate elements: (1, 2), (3, 4). On the third round zip pulls 5 for the first slot, then finds the iterator empty and stops – the 5 is consumed and lost, so list(it) is empty.

More Python Programming questions

Exams that ask this topic