ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
it = iter([3, 1, 0, 5, 0])
out = []
for x in iter(lambda: next(it), 0):
    out.append(x)
print(out, list(it))
  1. A. [3, 1] [5, 0]
  2. B. [3, 1, 0] [5, 0]
  3. C. [3, 1] [0]
  4. D. [3, 1] [5]

Correct answer: A. [3, 1] [5, 0]

iter(callable, sentinel) stops when the callable returns the sentinel, and it consumes that 0. The list holds 3 and 1, and the iterator still has 5 and 0.

More Python Programming questions

Exams that ask this topic