Grouping

Contents
  • Grouping by equality
  • The key parameter

The itertools groupby function divides a sequence into contiguous subsequences where the elements in each subsequence share something in common.

Grouping by equality

When used with a single parameter, groupby itertools.groupby finds groups of repeated elements. The reason this function is named “groupby” instead of “group” won’t become clear until we discuss the optional key parameter.

Sign up for access to the full page, plus the complete archive and all the latest content.