Use itertools.groupby:
>>> import itertools
>>> import operator
>>> foo = [("a", 1), ("a", 2), ("b", 1), ("c", 1), ("c", 2)]
>>> for group in itertools.groupby(foo, operator.itemgetter(0)):
... print group[0], list(map(operator.itemgetter(1), group[1]))
...
a [1, 2]
b [1]
c [1, 2]
Explanation:
groupby, , . , keyfunc , , keyfunc , , , . , ; , , groupby.
operator.itemgetter(0), "", x x[0]. , , .
, (, sys.stdin) . , , yield.
, , . , , : , , .