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.
# 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)) |
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.
# 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)) |
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]]
Recommended Posts:
- Python | Binary Group Tuple list elements
- Python | Group by matching second tuple value in list of tuples
- Python | Group list elements based on frequency
- Python | sort list of tuple based on sum
- Python | Remove duplicates based on Kth element tuple list
- Python | Sort tuple based on occurrence of first element
- Python | Sort tuple list by Nth element of tuple
- Python | Convert mixed data types tuple list to string list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Merge list of tuple into list by joining the strings
- Python | Pair and combine nested list to tuple list
- Python | Filter list of strings based on the substring list
- Python | Add list elements with a multi-list based on index
- Python | Convert list to indexed tuple list
- Python | Convert Integral list to tuple list
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.