Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Replace dictionary value from other dictionary

Python – Replace dictionary value from other dictionary

Given two dictionaries, update the values from other dictionary if key is present in other dictionary.

Input : test_dict = {“Gfg” : 5, “is” : 8, “Best” : 10, “for” : 8, “Geeks” : 9}, updict = {“Geeks” : 10, “Best” : 17} 
Output : {‘Gfg’: 5, ‘is’: 8, ‘Best’: 17, ‘for’: 8, ‘Geeks’: 10} 
Explanation : “Geeks” and “Best” values updated to 10 and 17. 

Input : test_dict = {“Gfg” : 5, “is” : 8, “Best” : 10, “for” : 8, “Geeks” : 9}, updict = {“Geek” : 10, “Bet” : 17} 
Output : {‘Gfg’: 5, ‘is’: 8, ‘Best’: 10, ‘for’: 8, ‘Geeks’: 9} 
Explanation : No values matched, hence original dictionary.

Method #1 : Using loop 

This is brute way in which this task can be performed. In this, we run a loop for each key in target dictionary and update in case the value is present in other dictionary.

Python3




# Python3 code to demonstrate working of
# Replace dictionary value from other dictionary
# Using loop
 
# initializing dictionary
test_dict = {"Gfg" : 5, "is" : 8, "Best" : 10, "for" : 8, "Geeks" : 9}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing updict
updict = {"Gfg"  : 10, "Best" : 17}
 
for sub in test_dict:
     
    # checking if key present in other dictionary
    if sub in updict:
        test_dict[sub]  = updict[sub]
 
# printing result
print("The updated dictionary: " + str(test_dict))


Output

The original dictionary is : {'Gfg': 5, 'is': 8, 'Best': 10, 'for': 8, 'Geeks': 9}
The updated dictionary: {'Gfg': 10, 'is': 8, 'Best': 17, 'for': 8, 'Geeks': 9}

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

Method #2 : Using dictionary comprehension

This is one liner approach in which this task can be performed. In this, we iterate for all the dictionary values and update in a one-liner manner in dictionary comprehension.

Python3




# Python3 code to demonstrate working of
# Replace dictionary value from other dictionary
# Using dictionary comprehension
 
# initializing dictionary
test_dict = {"Gfg" : 5, "is" : 8, "Best" : 10, "for" : 8, "Geeks" : 9}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing updict
updict = {"Gfg"  : 10, "Best" : 17}
 
res = {key: updict.get(key, test_dict[key]) for key in test_dict}
 
# printing result
print("The updated dictionary: " + str(res))


Output

The original dictionary is : {'Gfg': 5, 'is': 8, 'Best': 10, 'for': 8, 'Geeks': 9}
The updated dictionary: {'Gfg': 10, 'is': 8, 'Best': 17, 'for': 8, 'Geeks': 9}

Time complexity: O(n), 

Auxiliary space: O(m), 

Method #3: Using dict.update() method

This program updates the values of certain keys in a dictionary by using the update() method. It initializes two dictionaries (test_dict and updict), updates the values of the keys “Gfg” and “Best” in test_dict using the corresponding values in updict, and then prints the updated test_dict.

Python3




# Python3 code to demonstrate working of
# Replace dictionary value from other dictionary
# Using dict.update() method
 
# initializing dictionary
test_dict = {"Gfg" : 5, "is" : 8, "Best" : 10, "for" : 8, "Geeks" : 9}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing updict
updict = {"Gfg"  : 10, "Best" : 17}
 
# updating dictionary using dict.update() method
test_dict.update(updict)
 
# printing result
print("The updated dictionary: " + str(test_dict))


Output

The original dictionary is : {'Gfg': 5, 'is': 8, 'Best': 10, 'for': 8, 'Geeks': 9}
The updated dictionary: {'Gfg': 10, 'is': 8, 'Best': 17, 'for': 8, 'Geeks': 9}

Time complexity: O(m), where m is the size of the updict.

Auxiliary space: O(1), as the algorithm updates the existing dictionary in place and does not use any additional space proportional to the size of the input.

Method #5: Using the built-in map() function and a lambda function

In this method, we first create a list of updated values by mapping a lambda function to the keys of the original dictionary. The lambda function checks if the key is present in the second dictionary, and if it is, returns the corresponding value from the second dictionary. Otherwise, it returns the value from the original dictionary.

Python3




test_dict = {"Gfg": 5, "is": 8, "Best": 10, "for": 8, "Geeks": 9}
updict = {"Gfg": 10, "Best": 17}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
updated_values = map(
    lambda key: updict[key] if key in updict else test_dict[key], test_dict)
updated_dict = dict(zip(test_dict.keys(), updated_values))
test_dict = {"Gfg": 5, "is": 8, "Best": 10, "for": 8, "Geeks": 9}
updict = {"Gfg": 10, "Best": 17}
 
updated_values = map(
    lambda key: updict[key] if key in updict else test_dict[key], test_dict)
updated_dict = dict(zip(test_dict.keys(), updated_values))
 
print("The updated dictionary: " + str(updated_dict))


Output

The original dictionary is : {'Gfg': 5, 'is': 8, 'Best': 10, 'for': 8, 'Geeks': 9}
The updated dictionary: {'Gfg': 10, 'is': 8, 'Best': 17, 'for': 8, 'Geeks': 9}

The time complexity of this code is O(N), where N is the number of key-value pairs in the test_dict.
The auxiliary space complexity of this code is also O(N), as we use a map() object to store the updated values, and then we create a new dictionary using the zip() function.

Method 6: Using defaultdict

Use the defaultdict class from the collections module to create a new dictionary with default values set to the values of the original dictionary. We will then update the values of the keys present in the updict.

Step-by-step approach:

  • Create a defaultdict with default values from the original dictionary
  • Update the values of the keys present in the updict
  •  Convert the defaultdict back to a regular dictionary

Below is the implementation of the above approach:

Python3




from collections import defaultdict
 
test_dict = {"Gfg": 5, "is": 8, "Best": 10, "for": 8, "Geeks": 9}
updict = {"Gfg": 10, "Best": 17}
 
# create a defaultdict with default values from the original dictionary
default_dict = defaultdict(lambda: None, test_dict)
 
# update the values of the keys present in the updict
for key, value in updict.items():
    default_dict[key] = value
 
# convert the defaultdict back to a regular dictionary
updated_dict = dict(default_dict)
 
print("The updated dictionary: " + str(updated_dict))


Output

The updated dictionary: {'Gfg': 10, 'is': 8, 'Best': 17, 'for': 8, 'Geeks': 9}

Time complexity: O(N+M), where N is the number of keys in the original dictionary and M is the number of keys in the updict.
Auxiliary space: O(N), where N is the number of keys in the original dictionary.

RELATED ARTICLES

Most Popular

Recent Comments