Python · Iterators, modules and the standard library

itertools.groupby()

Groups consecutive items with the same key. Sort by the key first.

Syntax
itertools.groupby(iterable, key)

Example

Open in the lab

More in this group

collections.Counter collections.defaultdict collections.deque datetime enumerate + zip from … import Generator expression if __name__ == "__main__"

Learn this in the course

Reference pages are the quick answer. The course explains the why, shows the full example and ends with a challenge.

Open the course