groupby behaviour

A

andrea crotti

So I was trying to use groupby (which I used in the past), but I
noticed a very strange thing if using list on
the result:

In [109]: s = sorted(s, key=keyfunc)

In [110]: s
Out[110]: [(1, 2), (1, 3), (1, 5), (2, 3)]


In [117]: m2 = list(groupby(s, keyfunc))

In [118]: for name, res in m2:
.....: for r in res:
.....: print name, r
.....:
2 (2, 3)

In [119]: m2 = groupby(s, keyfunc)

In [120]: for name, res in m2:
for r in res:
print name, r
.....:
1 (1, 2)
1 (1, 3)
1 (1, 5)
2 (2, 3)

How is that possible?
Generating the list first should be the same as iterating after, how
can it behave differently?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top