# Python – Group Tuples by Kth Index Element

Sometimes, while working with Python records, we can have a problem in which we need to perform grouping of elements of tuple by similar Kth index element. This kind of problem can have application in web development domain. Let’s discuss certain way in which this task can be performed.

Input : test_list = [(4, 5), (3, 2), (2, 2), (1, 2), (5, 5)], K = 0
Output : [((1, 2), ), ((2, 2), ), ((3, 2), ), ((4, 5), ), ((5, 5), )]

Input : test_list = [(4, 5), (3, 2), (2, 2)], K = 1
Output : [((2, 2), (3, 2)), ((4, 5), )]

Method : Using `groupby() + itemegetter()` + generator expression
The combination of above functions can be used to solve this problem. In this, we perform the task of grouping the elements from Kth index extracted using itemgetter and generator expression is used to bind whole logic together.

 `# Python3 code to demonstrate working of  ` `# Group Tuples by Kth Index Element ` `# Using groupby() + itemegetter() + generator expression ` `from` `operator ``import` `itemgetter ` `from` `itertools ``import` `groupby ` ` `  `# initializing lists ` `test_list ``=` `[(``4``, ``5``), (``3``, ``2``), (``2``, ``2``), (``1``, ``2``), (``5``, ``5``)] ` ` `  `# printing original list ` `print``(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# initializing K  ` `K ``=` `1` ` `  `# Group Tuples by Kth Index Element ` `# Using groupby() + itemegetter() + generator expression ` `test_list.sort() ` `res ``=` `list``(``tuple``(sub) ``for` `idx, sub ``in` `groupby(test_list, key ``=` `itemgetter(K))) ` ` `  `# printing result  ` `print``(``"Tuples after grouping : "` `+` `str``(res)) `

Output :

```The original list is : [(4, 5), (3, 2), (2, 2), (1, 2), (5, 5)]
Tuples after grouping : [((1, 2), (2, 2), (3, 2)), ((4, 5), (5, 5))]
```

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.