Python – Group elements from Dual List Matrix

Sometimes, while working with Python list, we can have a problem in which we need to group the elements in list with the first element of Matrix and perform the Grouping in form of dictionary. This can have advantage in many domains. Lets discuss certain ways in which this task can be performed.

Method #1 : Using loop + list comprehension
The combination of above methods can be used in performing this task. In this, we iterate through the dual element row and compute the dictionary with mapping of elements from list and 2nd column of dual row matrix.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Group elements from Dual List Matrix
# using loop + list comprehension
  
# Initializing lists
test_list1 = ['Gfg', 'is', 'best']
test_list2 = [['Gfg', 1], ['is', 2], ['best', 1], ['Gfg', 4], ['is', 8], ['Gfg', 7]]
  
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
  
# Group elements from Dual List Matrix
# using loop + list comprehension
res = {key: [] for key in test_list1}
for key in res:
    res[key] = [sub[1] for sub in test_list2 if key == sub[0]]
              
# printing result 
print ("The dictionary after grouping : " + str(res))

chevron_right


Output :

The original list 1 is : [‘Gfg’, ‘is’, ‘best’]
The original list 2 is : [[‘Gfg’, 1], [‘is’, 2], [‘best’, 1], [‘Gfg’, 4], [‘is’, 8], [‘Gfg’, 7]]
The dictionary after grouping : {‘is’: [2, 8], ‘Gfg’: [1, 4, 7], ‘best’: [1]}

 



Method #2 : Using dictionary comprehension
This is yet another way in which this task can be performed. In this, we compile the logic performed above into one single dictionary comprehension for better readability.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Group elements from Dual List Matrix
# using dictionary comprehension
  
# Initializing lists
test_list1 = ['Gfg', 'is', 'best']
test_list2 = [['Gfg', 1], ['is', 2], ['best', 1], ['Gfg', 4], ['is', 8], ['Gfg', 7]]
  
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
  
# Group elements from Dual List Matrix
# using dictionary comprehension
res = {key: [sub[1] for sub in test_list2 if key == sub[0]] for key in test_list1}
              
# printing result 
print ("The dictionary after grouping : " + str(res))

chevron_right


Output :

The original list 1 is : [‘Gfg’, ‘is’, ‘best’]
The original list 2 is : [[‘Gfg’, 1], [‘is’, 2], [‘best’, 1], [‘Gfg’, 4], [‘is’, 8], [‘Gfg’, 7]]
The dictionary after grouping : {‘is’: [2, 8], ‘Gfg’: [1, 4, 7], ‘best’: [1]}




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.