Python | Group tuple into list based on value

Sometimes, while working with Python tuples, we can have a problem in which we need to group tuple elements to nested list on basis of values alloted to it. This can be useful in many grouping applications. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using itemgetter() + list comprehension + groupby()

The combination of above functions can be used to perform this task. In this, we access the value using itemgetter() and logic for grouping is performed using groupby() and list comprehension.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Group tuple into list based on value
# using itemgetter() + list comprehension + groupby()
from operator import itemgetter
from itertools import groupby
  
# initialize list 
test_list = [(1, 4), (2, 4), (6, 7), (5, 1), (6, 1), (8, 1)]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Group tuple into list based on value
# using itemgetter() + list comprehension + groupby()
res = [[i for i, j in temp]\
      for key, temp in groupby(test_list, key = itemgetter(1))]
  
# printing result
print("The list after grouping by value : " + str(res))

chevron_right


Output :

The original list : [(1, 4), (2, 4), (6, 7), (5, 1), (6, 1), (8, 1)]
The list after grouping by value : [[1, 2], [6], [5, 6, 8]]

 

Method #2 : Using map() + itemgetter() + groupby() + list comprehension

This method is similar to the above method, the only difference is that we chose map for formation of keys as nested list for formation of new resultant lists.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Group tuple into list based on value
# using map() + itemgetter() + groupby() + list comprehension
from operator import itemgetter
from itertools import groupby
  
# initialize list 
test_list = [(1, 4), (2, 4), (6, 7), (5, 1), (6, 1), (8, 1)]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Group tuple into list based on value
# using map() + itemgetter() + groupby() + list comprehension
res = [list(map(itemgetter(0), temp)) 
      for (key, temp) in groupby(test_list, itemgetter(1))]
  
# printing result
print("The list after grouping by value : " + str(res))

chevron_right


Output :

The original list : [(1, 4), (2, 4), (6, 7), (5, 1), (6, 1), (8, 1)]
The list after grouping by value : [[1, 2], [6], [5, 6, 8]]


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.