Skip to content
Related Articles

Related Articles

Python – Group Records on Similar index elements
  • Last Updated : 08 Jun, 2020

Sometimes, while working with Python records, we can have a problem in which we need to perform grouping of particular index of tuple, on basis of similarity of rest of indices. This type of problems can have possible application in Web development domain. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(4, 5, 10), (4, 5, 7), (4, 5, 12)]
Output : ((4, 5, [10, 7, 12]), )

Input : test_list = [(4, 5, 10)]
Output : [(4, 5, [10])]

Method #1 : Using defaultdict() + loop
The combination of above functions offer a brute approach to solve this problem. In this, we group elements and club into one using defaultdict() on basis of equality of rest of elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Group Records on Similar index elements
# Using defaultdict() + loop
from collections import defaultdict
  
# initializing list
test_list = [(4, 7, 13), (4, 5, 7), (6, 7, 10), (4, 5, 15), (6, 7, 12)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Group Records on Similar index elements
# Using defaultdict() + loop
temp = defaultdict(list)
for a, b, c in test_list:
    temp[a, b].append(c)
res = tuple((*key, val) for key, val in temp.items())
  
# printing result 
print("Tuples after grouping : " + str(res)) 

chevron_right


Output :



The original list is : [(4, 7, 13), (4, 5, 7), (6, 7, 10), (4, 5, 15), (6, 7, 12)]
Tuples after grouping : ((4, 7, [13]), (4, 5, [7, 15]), (6, 7, [10, 12]))

 

Method #2 : Using groupby() + generator expression
The combinations of above functions can be used to solve this problem. In this, we perform the grouping of all elements using groupby() and generation of tuple list using generator expression. Needs sorted list for this method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Group Records on Similar index elements
# Using groupby() + generator expression
from itertools import groupby
  
# initializing list
test_list = [(4, 7, 13), (4, 5, 7), (6, 7, 10), (4, 5, 15), (6, 7, 12)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Group Records on Similar index elements
# Using groupby() + generator expression
test_list.sort()
res = tuple((*key, [tup[-1] for tup in val]) for key, val in groupby(test_list, lambda tup: tup[:2]))
  
# printing result 
print("Tuples after grouping : " + str(res)) 

chevron_right


Output :

The original list is : [(4, 7, 13), (4, 5, 7), (6, 7, 10), (4, 5, 15), (6, 7, 12)]
Tuples after grouping : ((4, 7, [13]), (4, 5, [7, 15]), (6, 7, [10, 12]))

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :