Sometimes, while working with Python list, we can have a problem in which we need to group similar value lists indices to values into a dictionary. This can have a good application in domains in which we need grouped dictionary as output for pair of lists. Lets discuss certain ways in which this task can be performed.
Method #1 : Using dictionary comprehension
This is way in which this task can be performed. In this, we iterate one list along with other and keep constructing dictionary with similar key from one list and corresponding values from other. This is one liner solution.
# Python3 code to demonstrate # Group similar value list to dictionary # using dictionary comprehension # Initializing lists test_list1 = [4, 4, 4, 5, 5, 6, 6, 6, 6] test_list2 = ['G', 'f', 'g', 'i', 's', 'b', 'e', 's', 't'] # printing original lists print("The original list 1 is : " + str(test_list1)) print("The original list 2 is : " + str(test_list2)) # Group similar value list to dictionary # using dictionary comprehension res = {key : [test_list2[idx] for idx in range(len(test_list2)) if test_list1[idx]== key] for key in set(test_list1)} # printing result print ("Mapped resultant dictionary : " + str(res)) |
The original list 1 is : [4, 4, 4, 5, 5, 6, 6, 6, 6]
The original list 2 is : [‘G’, ‘f’, ‘g’, ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’]
Mapped resultant dictionary : {4: [‘G’, ‘f’, ‘g’], 5: [‘i’, ‘s’], 6: [‘b’, ‘e’, ‘s’, ‘t’]}
Method #2 : Using defaultdict() + loop
This is yet another way in which this task can be performed. In this we avoid testing for keys presence in dictionary by creating a defaultdict().
# Python3 code to demonstrate # Group similar value list to dictionary # using defaultdict() + loop from collections import defaultdict # Initializing lists test_list1 = [4, 4, 4, 5, 5, 6, 6, 6, 6] test_list2 = ['G', 'f', 'g', 'i', 's', 'b', 'e', 's', 't'] # printing original lists print("The original list 1 is : " + str(test_list1)) print("The original list 2 is : " + str(test_list2)) # Group similar value list to dictionary # using defaultdict() + loop res = defaultdict(set) for key, val in zip(test_list1, test_list2): res[key].add(val) res = {key: list(val) for key, val in res.items()} # printing result print ("Mapped resultant dictionary : " + str(res)) |
The original list 1 is : [4, 4, 4, 5, 5, 6, 6, 6, 6]
The original list 2 is : [‘G’, ‘f’, ‘g’, ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’]
Mapped resultant dictionary : {4: [‘G’, ‘f’, ‘g’], 5: [‘i’, ‘s’], 6: [‘b’, ‘e’, ‘s’, ‘t’]}
Recommended Posts:
- Python - Group Similar items to Dictionary Values List
- Python - Group Similar keys in dictionary
- Python program to group keys with similar values in a dictionary
- Python - Convert Dictionary Value list to Dictionary List
- Python | Group and count similar records
- Python - Group Similar Start and End character words
- Python - Group Records on Similar index elements
- Python - Group similar elements into Matrix
- Group List of Dictionary Data by Particular Key in Python
- Python | Pretty Print a dictionary with dictionary value
- Python - Replace dictionary value from other dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python - Pairs with multiple similar values in dictionary
- Combine similar characters in Python using Dictionary Get() Method
- Python | Add similar value multiple times in list
- Python | Group by matching second tuple value in list of tuples
- Python | Group tuples in list with same first value
- Python | Group tuple into list based on value
- Python program to update a dictionary with the values from a dictionary list
- Python - Convert Key-Value list Dictionary to List of Lists
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.

