Search code examples
pythonpython-itertools

How to use python groupby()


When I try to use itertools.groupby to group a list of numbers like this:

from itertools import groupby

a = [1, 2, 1, 3, 2, 1, 2, 3, 4, 5]

for key, value in groupby(a):
    print((len(list(value)), key), end=' ')

The output is

(1, 1) (1, 2) (1, 1) (1, 3) (1, 2) (1, 1) (1, 2) (1, 3) (1, 4) (1, 5) 

instead of

(3, 1) (3, 2) (2, 3) (1, 4) (1, 5)

Why doesn't it group identical numbers correctly?


Solution

  • Grouping input by common key elements with groupby() only works on input already sorted by that key:

    [...] Generally, the iterable needs to already be sorted on the same key function.

    Your example should work like this:

    from itertools import groupby
    
    a = sorted([1, 2, 1, 3, 2, 1, 2, 3, 4, 5])
    
    for key, value in groupby(a):
        print((len(list(value)), key), end=' ')
    

    If you use groupby() on unorderd input you'll get a new group every time a different key is returned by the key function while iterating through the iterable.