Python | Convert list of tuples to dictionary value lists
One among the problem of interconversion of data types in python is conversion of list of tuples to dictionaries, in which the keys are 1st element of tuple, which are uniquely identified as keys in dictionary and it’s corresponding value as list of corresponding value of respective keys as tuple’s second element. Let’s discuss how to solve this particular problem.
Method #1 : Using defaultdict() + loop
This problem can easily be solved using the defaultdict() and loop. The defaultdict provides a default value dictionary container to assign corresponding value list to keys, so that we don’t need to initialize keys with empty list and loop is used to extract like values from tuple.
# Python3 code to demonstrate working of # Convert list of tuples to dictionary value lists # Using defaultdict() + loop from collections import defaultdict # initializing list test_list = [(1, 'gfg'), (1, 'is'), (2, 'best'), (3, 'for'), (4, 'CS')] # printing original list print("The original list is : " + str(test_list)) # Using defaultdict() + loop # Convert list of tuples to dictionary value lists res = defaultdict(list) for i, j in test_list: res[i].append(j) # printing result print("The merged dictionary is : " + str(dict(res))) |
The original list is : [(1, ‘gfg’), (1, ‘is’), (2, ‘best’), (3, ‘for’), (4, ‘CS’)]
The merged dictionary is : {1: [‘gfg’, ‘is’], 2: [‘best’], 3: [‘for’], 4: [‘CS’]}
Method #2 : Using defaultdict() + groupby()
This method performs the task similar to above method, just instead of using loop to access each key value pair, here we use Python’s groupby library to group keys with value and convert then into a list.
# Python3 code to demonstrate working of # Convert list of tuples to dictionary value lists # Using defaultdict() + groupby() from collections import defaultdict from operator import itemgetter from itertools import groupby # initializing list test_list = [(1, 'gfg'), (1, 'is'), (2, 'best'), (3, 'for'), (4, 'CS')] # printing original list print("The original list is : " + str(test_list)) # Using defaultdict() + groupby() # Convert list of tuples to dictionary value lists res = dict((k, [v[1] for v in itr]) for k, itr in groupby( test_list, itemgetter(0))) # printing result print("The merged dictionary is : " + str(dict(res))) |
The original list is : [(1, ‘gfg’), (1, ‘is’), (2, ‘best’), (3, ‘for’), (4, ‘CS’)]
The merged dictionary is : {1: [‘gfg’, ‘is’], 2: [‘best’], 3: [‘for’], 4: [‘CS’]}
Recommended Posts:
- Python | Convert dictionary to list of tuples
- Python | Convert a list of Tuples into Dictionary
- Python | Convert Tuples to Dictionary
- Python | Convert string tuples to list tuples
- Python | Convert Lists to column tuples
- Python | List of tuples to dictionary conversion
- Python | Convert two lists into a dictionary
- Python | Merge two lists into list of tuples
- Python | Convert list of tuples to list of strings
- Python | Convert list of strings to list of tuples
- Python | Convert list of tuples into digits
- Python | Convert list elements to bi-tuples
- Python | Convert String to list of tuples
- Python | Convert list of tuples into list
- Python | Convert list of tuples to list of 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.



