ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import itertools
g = itertools.groupby('aab')
k, grp = next(g)
next(g)
print(k, list(grp))
  1. A. a ['a', 'a']
  2. B. a ['a']
  3. C. None []
  4. D. a []

Correct answer: D. a []

groupby shares one underlying iterator. Advancing to the next group with next(g) skips the rest of the first group, so the earlier group iterator is empty when read afterwards.

More Python Programming questions

Exams that ask this topic