ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import itertools
print(list(itertools.batched(range(7), 3))[-1])
  1. A. [5, 6]
  2. B. (4, 5, 6)
  3. C. 7
  4. D. (6,)

Correct answer: D. (6,)

batched (Python 3.12+) yields tuples of at most n items. The last batch holds only the leftover item, 6, and is returned as the 1-tuple (6,).

More Python Programming questions

Exams that ask this topic