I'm using Python's itertools.groupby to count consecutive occurrences of elements as mentioned below
from itertools import groupby
data = [1, 1, 3, 2, 3, 3, 4, 5, 5]
sorted_data = sorted(data)
groups = groupby(sorted_data)
for key, group in groups:
print(key, len(list(group)))
It produces the output as expected
1 2
2 1
3 3
4 1
5 2
But out of curiosity I tried to print the counts alone just to see what happens like below,
from itertools import groupby
data = [1, 1, 3, 2, 3, 3, 4, 5, 5]
sorted_data = sorted(data)
groups = groupby(sorted_data)
for group in groups:
print(len(list(group)))
I get this response
2
2
2
2
2
I've encountered that the groupby function behaves differently when I try to print the counts alone versus when I print both the key and the count.
Why does groupby behave differently in these two cases? an explanation will be great.
Thanks in advance for your help!
your line
will always give 2. as group is a list of 2 items, where the first item is the index, and the second item is the iterator.
i.e. your group is
Instead replace it with