Open In App

Python | Merge Python key values to list

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python, we might have a problem in which we need to get the values of dictionary from several dictionaries to be encapsulated into one dictionary. This type of problem can be common in domains in which we work with relational data like in web developments. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using setdefault() + loop This task can be performed using a nested loop and fetching each element of dictionary and creating a new list to new key or appending the values in case of similar key occurrence. 

Python3




# Python3 code to demonstrate working of
# Merge Python key values to list
# Using setdefault() + loop
 
# Initialize list
test_list = [{'gfg' : 2, 'is' : 4, 'best' : 6},
             {'it' : 5, 'is' : 7, 'best' : 8},
             {'CS' : 10}]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# using setdefault() + loop
# Merge Python key values to list
res = {}
for sub in test_list:
    for key, val in sub.items():
        res.setdefault(key, []).append(val)
 
# printing result
print("The merged values encapsulated dictionary is : " + str(res))


Output : 

The original list is : [{‘is’: 4, ‘gfg’: 2, ‘best’: 6}, {‘it’: 5, ‘is’: 7, ‘best’: 8}, {‘CS’: 10}] The merged values encapsulated dictionary is : {‘is’: [4, 7], ‘it’: [5], ‘gfg’: [2], ‘CS’: [10], ‘best’: [6, 8]}

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using list comprehension + dictionary comprehension The combination of above can be used to perform this particular task. This offers a one liner that can be employed for this task. Even though it might be efficient on performance domain. 

Python3




# Python3 code to demonstrate working of
# Merge Python key values to list
# Using list comprehension + dictionary comprehension
 
# Initialize list
test_list = [{'gfg' : 2, 'is' : 4, 'best' : 6},
             {'it' : 5, 'is' : 7, 'best' : 8},
             {'CS' : 10}]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# using list comprehension + dictionary comprehension
# Merge Python key values to list
res = {key: list({sub[key] for sub in test_list if key in sub})
      for key in {key for sub in test_list for key in sub}}
 
# printing result
print("The merged values encapsulated dictionary is : " + str(res))


Output : 

The original list is : [{‘is’: 4, ‘gfg’: 2, ‘best’: 6}, {‘it’: 5, ‘is’: 7, ‘best’: 8}, {‘CS’: 10}] The merged values encapsulated dictionary is : {‘is’: [4, 7], ‘it’: [5], ‘gfg’: [2], ‘CS’: [10], ‘best’: [6, 8]}

The time complexity of this method is O(n^2) where n is the number of elements in the input list of dictionaries. 

The auxiliary space complexity of this method is also O(n^2), because it creates a new set for every key in every dictionary, and a new list for every unique value.

Method 3: Using defaultdict

  • Initialize list
  • Printing original list
  • Using defaultdict Merge Python key values to list
  • Convert defaultdict to a regular dictionary

Python3




from collections import defaultdict
 
# Initialize list
test_list = [{'gfg' : 2, 'is' : 4, 'best' : 6},
             {'it' : 5, 'is' : 7, 'best' : 8},
             {'CS' : 10}]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# using defaultdict
# Merge Python key values to list
res = defaultdict(list)
for sub in test_list:
    for key, val in sub.items():
        res[key].append(val)
 
# convert defaultdict to a regular dictionary
res = dict(res)
 
# printing result
print("The merged values encapsulated dictionary is : " + str(res))


Output

The original list is : [{'gfg': 2, 'is': 4, 'best': 6}, {'it': 5, 'is': 7, 'best': 8}, {'CS': 10}]
The merged values encapsulated dictionary is : {'gfg': [2], 'is': [4, 7], 'best': [6, 8], 'it': [5], 'CS': [10]}

Time Complexity: O(n)
Auxiliary Space: O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads