How to join second elements in tuples using group by first element python

Question:

I have a list of tuples

a = [('name', 'color'),('fruit', 'color'),('thing', 'type'),('sport', 'type')]

I want to join first elements grouped by second element. The output should look like.

a = [('name fruit', 'color'),('thing sport', 'type')]
Asked By: Anonymous

||

Answers:

You can create dict with the key that you want to group and add value to list base key. For this approach, you can use collections.defaultdict or if you don’t want to import anything you can use dict.setdefault.

from collections import defaultdict


a = [('name', 'color'),('fruit', 'color'),('thing', 'type'),('sport', 'type')]
res = defaultdict(list)
res_2 = {}

for tpl in a:
    res[tpl[1]].append(tpl[0])
    res_2.setdefault(tpl[1], []).append(tpl[0])

# Now you can use `res_2` instead of `res`
lst = [(' '.join(v), k) for k,v  in res.items()]
print(lst)

Another option can be to use itertools.groupby. For this approach, you can set with which value of your tuple you want to group. (Because you want to group the base second value of each tuple you can set the key of groupby like lambda x: x[1].)

from itertools import groupby

res = []
for key, group in itertools.groupby(a, lambda x: x[1]):
    res.append((' '.join(tpl[0] for tpl in group), key))
print(res)

Output:

[('name fruit', 'color'), ('thing sport', 'type')]
Answered By: I'mahdi

No imports needed by probably not super fast:

new = [(a[k][0] + " " + a[k+1][0], a[k][1]) for k in range(0, len(a), 2)]
Answered By: JustLearning

You can use itertools.groupby() and operator.itemgetter() in a list comprehension to achieve this as:

from itertools import groupby
from operator import itemgetter

my_list = [('name', 'color'),('fruit', 'color'),('thing', 'type'),('sport', 'type')]

new_list = [(' '.join(item[0] for item in g_list), x) for x, g_list in groupby(my_list, itemgetter(1))] 

#   where `new_list` will be holding the value:
#       [('name fruit', 'color'), ('thing sport', 'type')]
Answered By: Moinuddin Quadri
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.