# How do I count sequences in Python with itertools groupby?

I'm trying to use the itertools groupby function here:
https://docs.python.org/2/library/itertools.html#itertools.groupby

but there are no examples of how to do this with count.  Doing the following:
``````import itertools as itr
In [204]: list(itr.groupby('1222311'))
Out[204]:
[('1', <itertools._grouper at 0x3cae048>),
('2', <itertools._grouper at 0x3cae630>),
('3', <itertools._grouper at 0x3caedd8>),
('1', <itertools._grouper at 0x3cae1d0>)]
``````
yields some indecipherable second element of each tuple.  What I want to get back from that is:
In [204]: list(itr.groupby('1222311'))
Out[204]:
``````[('1', 1),
('2', 3),
('3', 1),
('1', 2)]
``````
though the order of elements within the tuples is irrelevant.
I tried adding a second argument to the groupby (e.g. key=itr.count), but they all give errors.  How do I accomplish this and how do I use the keyfunc parameter of groupby?
LVL 11
###### Who is Participating?

x

Senior Software EngineerCommented:
The undecipherable object grouper is a lazy iterator, so in order to get the count for each of those elements, you need to get their len.

``````import itertools as itr
for a, b  in itr.groupby('1222311'):
print("(%s,%s)" % (a, sum(1 for i in b)))
``````

you could also write like this:
``````import itertools as itr
for a, b  in itr.groupby('1222311'):
print("(%s,%s)" % (a, len(list(b))))
``````
0

Author Commented:
Thank you, that's helpful.  What exactly is the iter group iterating? We already have the token, and then you're iterating over something that gives you a count.  It looks like, after some testing, that it's just the elements of the group.  I guess that's more useful in different scenarios with a more complicated grouping function.

How do I use the keyfunc parameter?  What functions would work there?  I've tried many different things, and they all give errors.
0

Author Commented:
Okay, thanks to your help I figured out how to do it as a single list.  This is what I wanted to do:

[ (i[0],len(list(i[1]))) for i in itr.groupby('1113334')]
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.