Open In App

Python program to update a dictionary with the values from a dictionary list

Given a dictionary and dictionary list, update the dictionary with dictionary list values.

Input : test_dict = {“Gfg” : 2, “is” : 1, “Best” : 3}, dict_list = [{‘for’ : 3, ‘all’ : 7}, {‘and’ : 1, ‘CS’ : 9}] 
Output : {‘Gfg’: 2, ‘is’: 1, ‘Best’: 3, ‘for’: 3, ‘all’: 7, ‘and’: 1, ‘CS’: 9} 
Explanation : All dictionary keys updated in single dictionary.



Input : test_dict = {“Gfg” : 2, “is” : 1, “Best” : 3}, dict_list = [{‘for’ : 3, ‘all’ : 7}] 
Output : {‘Gfg’: 2, ‘is’: 1, ‘Best’: 3, ‘for’: 3, ‘all’: 7} 
Explanation : All dictionary keys updated in single dictionary. 

Method #1 : Using update() + loop



In this, we iterate through all the elements in loop, and perform update to update all the keys in dictionary to original dictionary.

Step-by-step approach :

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Update dictionary with dictionary list
# Using update() + loop
 
# initializing dictionary
test_dict = {"Gfg" : 2, "is" : 1, "Best" : 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing dictionary list
dict_list = [{'for' : 3, 'all' : 7}, {'geeks' : 10}, {'and' : 1, 'CS' : 9}]
 
for dicts in dict_list:
     
    # updating using update()
    test_dict.update(dicts)
 
# printing result
print("The updated dictionary : " + str(test_dict))

Output
The original dictionary is : {'Gfg': 2, 'is': 1, 'Best': 3}
The updated dictionary : {'Gfg': 2, 'is': 1, 'Best': 3, 'for': 3, 'all': 7, 'geeks': 10, 'and': 1, 'CS': 9}

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

Method #2 : Using ChainMap + ** operator

In this, we perform task of merging all list dictionaries into 1 using ChainMap and ** operator is used to merge target dictionary to merged dictionary.




# Python3 code to demonstrate working of
# Update dictionary with dictionary list
# Using ChainMap + ** operator
from collections import ChainMap
 
# initializing dictionary
test_dict = {"Gfg" : 2, "is" : 1, "Best" : 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing dictionary list
dict_list = [{'for' : 3, 'all' : 7}, {'geeks' : 10}, {'and' : 1, 'CS' : 9}]
 
# ** operator extracts keys and re initiates.
# ChainMap is used to merge dictionary list
res = {**test_dict, **dict(ChainMap(*dict_list))}
 
# printing result
print("The updated dictionary : " + str(res))

Output
The original dictionary is : {'Gfg': 2, 'is': 1, 'Best': 3}
The updated dictionary : {'Gfg': 2, 'is': 1, 'Best': 3, 'and': 1, 'CS': 9, 'geeks': 10, 'for': 3, 'all': 7}

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

Method 3: Using dictionary comprehension.

Using dictionary comprehension to create a new dictionary from the list of dictionaries. We are using the unpacking operator (**), which unpacks the key-value pairs from both dictionaries into a single dictionary.

Follow the below steps to implement the above idea:

Below is the implementation of the above approach:




# initializing dictionary
test_dict = {"Gfg": 2, "is": 1, "Best": 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing dictionary list
dict_list = [{'for': 3, 'all': 7}, {'geeks': 10}, {'and': 1, 'CS': 9}]
 
# Using dictionary comprehension to update the dictionary
test_dict = {**test_dict, **
             {key: val for dicts in dict_list for key, val in dicts.items()}}
 
# printing result
print("The updated dictionary : " + str(test_dict))

Output
The original dictionary is : {'Gfg': 2, 'is': 1, 'Best': 3}
The updated dictionary : {'Gfg': 2, 'is': 1, 'Best': 3, 'for': 3, 'all': 7, 'geeks': 10, 'and': 1, 'CS': 9}

Time complexity: O(nm), where n is the length of the list of dictionaries and m is the average number of key-value pairs in each dictionary.
Auxiliary space: O(nm), as we are creating a new dictionary to store the updated key-value pairs.

Method 4: Using a list comprehension and the update() 

Step-by-step approach:




# initializing dictionary
test_dict = {"Gfg": 2, "is": 1, "Best": 3}
 
# printing original dictionary
print("The original dictionary is: " + str(test_dict))
 
# initializing dictionary list
dict_list = [{'for': 3, 'all': 7}, {'geeks': 10}, {'and': 1, 'CS': 9}]
 
# Using list comprehension and update() method to update the dictionary
[test_dict.update(d) for d in dict_list]
 
# printing result
print("The updated dictionary: " + str(test_dict))

Output
The original dictionary is: {'Gfg': 2, 'is': 1, 'Best': 3}
The updated dictionary: {'Gfg': 2, 'is': 1, 'Best': 3, 'for': 3, 'all': 7, 'geeks': 10, 'and': 1, 'CS': 9}

Time complexity: O(n*m), where n is the length of the dict_list and m is the average length of the dictionaries in dict_list.
Auxiliary space: O(1), since the only extra space used is for the test_dict dictionary and the dict_list list, both of which are already initialized.

Method 5: Using a for loop to iterate over the dictionary list and its items and adding them to the original dictionary

Step-by-step approach:

Below is the implementation of the above approach:




# initializing dictionary
test_dict = {"Gfg": 2, "is": 1, "Best": 3}
 
# printing original dictionary
print("The original dictionary is: " + str(test_dict))
 
# initializing dictionary list
dict_list = [{'for': 3, 'all': 7}, {'geeks': 10}, {'and': 1, 'CS': 9}]
 
# Using for loop to update the dictionary
for d in dict_list:
    for key, value in d.items():
        test_dict[key] = value
 
# printing result
print("The updated dictionary: " + str(test_dict))

Output
The original dictionary is: {'Gfg': 2, 'is': 1, 'Best': 3}
The updated dictionary: {'Gfg': 2, 'is': 1, 'Best': 3, 'for': 3, 'all': 7, 'geeks': 10, 'and': 1, 'CS': 9}

Time complexity: O(N*M), where N is the length of the dictionary list and M is the average number of key-value pairs in each dictionary in the list.
Auxiliary space: O(1), as we are only modifying the original dictionary in place and not creating any additional data structures.

Method 6: Using the built-in function reduce() from the functools module and a lambda function

Step-by-step approach :

Below is the implementation of the above approach:




# Import functools module
from functools import reduce
 
# Define lambda function
update_dict = lambda x, y: x.update(y) or x
 
# Initializing dictionary
test_dict = {"Gfg": 2, "is": 1, "Best": 3}
 
# Print original dictionary
print("The original dictionary is: " + str(test_dict))
 
# Initializing dictionary list
dict_list = [{'for': 3, 'all': 7}, {'geeks': 10}, {'and': 1, 'CS': 9}]
 
# Using reduce and lambda function to update the dictionary
test_dict = reduce(update_dict, dict_list, test_dict)
 
# Print the updated dictionary
print("The updated dictionary: " + str(test_dict))

Output
The original dictionary is: {'Gfg': 2, 'is': 1, 'Best': 3}
The updated dictionary: {'Gfg': 2, 'is': 1, 'Best': 3, 'for': 3, 'all': 7, 'geeks': 10, 'and': 1, 'CS': 9}

Time complexity: O(nm), where n is the length of dict_list and m is the maximum number of key-value pairs in any dictionary in dict_list.
Auxiliary space: O(1), because we are updating the original dictionary in place and not creating any new data structures.


Article Tags :