# Python – Group Records on Similar index elements

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, )]

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.

 `# 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))  `

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, ), (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.

 `# 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))  `

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, ), (4, 5, [7, 15]), (6, 7, [10, 12]))
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.