0

I am trying to group data based on second argument.

test= [(1,1),(3,1),(5,0),(3,0),(2,1)]

I am excuting code from this thread

from itertools import groupby
from operator import itemgetter
result = [list(group) for key, group in groupby(test, itemgetter(1))]

But instead of getting a result that look like: [[(1,1),(3,1),(2,1)],[(5,0),(3,0)]]

Why I am getting the last tuple in a seperated list, while I should get it grouped with other values where 1 is a second values

[[(1,1),(3,1)],[(5,0),(3,0)],[(2,1)]]

Community
  • 1
  • 1
user3378649
  • 5,154
  • 14
  • 52
  • 76
  • 3
    Because `groupby` groups only adjacent items. It is not equivalent to SQL's groupby. – Ashwini Chaudhary Dec 17 '14 at 20:54
  • Related: [How to group a list of tuples/objects by similar index/attribute in python?](http://stackoverflow.com/questions/6602172/how-to-group-a-list-of-tuples-objects-by-similar-index-attribute-in-python) – Ashwini Chaudhary Dec 17 '14 at 20:57

1 Answers1

1

This has tripped me up in the past as well. If you want it to group globally, it's best to sort the list first:

In [163]: test = [(1,1),(3,1),(5,0),(3,0),(2,1)]

In [164]: crit = operator.itemgetter(1)

In [165]: test.sort(key=crit)

In [166]: result = [list(group) for key, group in itertools.groupby(test, crit)]

In [167]: result
Out[167]: [[(5, 0), (3, 0)], [(1, 1), (3, 1), (2, 1)]]
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241