ProgrammingHardTCS NQT · Wipro pattern

Which statement about itertools.groupby is correct?

  1. A. It groups only consecutive items that have equal keys
  2. B. It sorts its input before grouping
  3. C. It returns a dictionary of lists
  4. D. It works only on lists and not on other iterables

Correct answer: A. It groups only consecutive items that have equal keys

groupby walks the input once and starts a new group whenever the key changes, so unsorted input gives repeated groups. It does not sort, build a dict, or require a list.

More Python Programming questions

Exams that ask this topic