views:

2871

answers:

5

Hi,

I have a list of tuples like this:

[
('a', 1),
('a', 2),
('a', 3),
('b', 1),
('b', 2),
('c', 1),
]

I want to iterate through this keying by the first item, so for example I could print something like this:

a 1 2 3
b 1 2
c 1

How would I go about doing this without keeping an item to track whether the first item is the same as I loop round the tuples. This feels rather messy (plus I have to sort the list to start with)...

Thanks,

Dan

+20  A: 
l = [
('a', 1),
('a', 2),
('a', 3),
('b', 1),
('b', 2),
('c', 1),
]

d = {}
for x, y in l:
    d.setdefault(x, []).append(y)
print d

produces:

{'a': [1, 2, 3], 'c': [1], 'b': [1, 2]}
Ned Batchelder
+12  A: 

Slightly simpler...

>>> from collections import defaultdict
>>> fq= defaultdict( list )
>>> for n,v in myList:
        fq[n].append(v)

>>> fq
defaultdict(<type 'list'>, {'a': [1, 2, 3], 'c': [1], 'b': [1, 2]})
S.Lott
My approach would've been similar to Ned Batchelder's but this is much nicer.
Blair Conrad
@Blair Conrad: Ned Batchelder's was easier to type: he beat me to the post by several minutes.
S.Lott
Haha! type, or look up the collections API. Thanks though, I learned something new.
Ali A
collections.defaultdict is one of my all-time favorite classes. It's the answer to questions approximately as frequently as a "Use VLOOKUP()" is the answer to Excel questions.
hughdbrown
+6  A: 

A solution using groupby

    >>> from itertools import groupby
    >>> l = [('a',1), ('a', 2),('a', 3),('b', 1),('b', 2),('c', 1),]
    >>> [(label, [v for l,v in value]) for (label, value) in groupby(l, lambda x:x[0])]
    [('a', [1, 2, 3]), ('b', [1, 2]), ('c', [1])]

groupby(l, lambda x:x[0]) gives you an iterator that contains ['a', [('a', 1), ...], c, [('c', 1)], ...]

You should probably mention that you need a sorted list for this (or use groupby(sorted(l),.. )for this to work. The data given looks sorted, but the question mentions "(plus I have to sort the list to start with)..." so I'm not sure you can rely on this.
Brian
+1  A: 

I would just do the basic

answer = {}
for key, value in list_of_tuples:
  if key in answer:
    answer[key].append(value)
  else:
    answer[key] = [value]

If it's this short, why use anything complicated. Of course if you don't mind using setdefault that's okay too.

lacker
A: 

Print list of tuples grouping by the first item

This answer is based on the @gommen one.

#!/usr/bin/env python

from itertools import groupby
from operator  import itemgetter

L = [
('a', 1),
('a', 2),
('a', 3),
('b', 1),
('b', 2),
('c', 1),
]

key = itemgetter(0)
L.sort(key=key) #NOTE: use `L.sort()` if you'd like second items to be sorted too
for k, group in groupby(L, key=key):
    print k, ' '.join(str(item[1]) for item in group)

Output:

a 1 2 3
b 1 2
c 1
J.F. Sebastian