Reputation: 139
Given a list of tuples like so:
a = [ ( "x", 1, ), ( "x", 2, ), ( "y", 1, ), ( "y", 3, ), ( "y", 4, ) ]
What would be the easiest way to filter for unique first element and merge the second element. An output like so would be desired.
b = [ ( "x", 1, 2 ), ( "y", 1, 3, 4 ) ]
Thanks,
Upvotes: 5
Views: 5002
Reputation: 48090
One way is by using list comprehension expression with itertools.groupby
, itertools.chain
and operator.itemgetter
as:
>>> from itertools import groupby, chain
>>> from operator import itemgetter
>>> my_list = [ ( "x", 1, ), ( "x", 2, ), ( "y", 1, ), ( "y", 3, ), ( "y", 4, ) ]
>>> [set(chain(*i)) for _, i in groupby(sorted(my_list), key=itemgetter(0))]
[set(['x', 2, 1]), set(['y', 1, 3, 4])]
Note: set
are unordered in nature, so they won't preserve the position of elements. Do not use set
if the position matters.
Upvotes: 0
Reputation: 117475
>>> a = [("x", 1,), ("x", 2,), ("y", 1,), ("y", 3,), ("y", 4,)]
>>> d = {}
>>> for k, v in a:
... d.setdefault(k, [k]).append(v)
>>> b = map(tuple, d.values())
>>> b
[('y', 1, 3, 4), ('x', 1, 2)]
Upvotes: 5
Reputation: 9726
In addition to previous answers, another one-liner:
>>> a = [ ( "x", 1, ), ( "x", 2, ), ( "y", 1, ), ( "y", 3, ), ( "y", 4, ) ]
>>> from itertools import groupby
>>> [(key,) + tuple(elem for _, elem in group) for key, group in groupby(a, lambda pair: pair[0])]
[('x', 1, 2), ('y', 1, 3, 4)]
Upvotes: 0
Reputation: 17871
This is what I came up with:
[tuple(list(el) + [q[1] for q in a if q[0]==el]) for el in set([q[0] for q in a])]
Upvotes: 1
Reputation: 60004
You can use a defaultdict
:
>>> from collections import defaultdict
>>> d = defaultdict(tuple)
>>> a = [('x', 1), ('x', 2), ('y', 1), ('y', 3), ('y', 4)]
>>> for tup in a:
... d[tup[0]] += (tup[1],)
...
>>> [tuple(x for y in i for x in y) for i in d.items()]
[('y', 1, 3, 4), ('x', 1, 2)]
Upvotes: 2