Function in the itertools module. Makes an iterator that returns **consecutive keys** and groups from the iterable (note the difference from similarly named GROUPBY in SQL, pandas etc. which groups rows by the same values). Be sure to include the `python` tag for increased visibility.
groupby
method in the itertools
module makes an iterator that returns groups from an iterable.
Unlike the similarly named GROUPBY in SQL databases or data manipulation packages such as pandas (which group by common elements regardless of order), itertools.groupby()
creates a new group every time the value of the key changes. For example, [1,1,2,1]
is three groups: [[1,1], [2], [1]]
. To mimic the GROUPBY in SQL, it is often needed to sort the input by the keys to be grouped on.
Reference / related tags: