Sometimes, while working with Python dictionaries we can have a problem in which we need to perform a population of dictionary values using assigned variables after certain operation among them. This can have application in day-day programming. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using lambda + dictionary comprehension
The combination of above functions can be used to solve this problem. In this, we perform the assignment using dictionary comprehension and values computations using lambda functions.
Python3
helper_fnc = { 'Gfg' : lambda : x + y,
'best' : lambda : x * y}
x = 6
y = 7
res = {key: val() for key, val in hlper_fnc.items()}
print ( "The Initialized dictionary : " + str (res))
|
Output : The Initialized dictionary : {'best': 42, 'Gfg': 13}
Time complexity: O(n), where n is the number of values in the dictionary.
Auxiliary Space: O(1), constant extra space is required
Method #2 : Using operators library
This task can also be performed using above functionality. In this, we use inbuilt operations provided by the operator library to achieve this task.
Python3
from operator import add, mul
helper_fnc = { 'Gfg' : add,
'best' : mul}
x = 6
y = 7
res = { 'Gfg' : hlper_fnc[ 'Gfg' ](x, y), 'best' : hlper_fnc[ 'best' ](x, y)}
print ( "The Initialized dictionary : " + str (res))
|
Output : The Initialized dictionary : {'best': 42, 'Gfg': 13}
Method 3: Using the update() method:
Algorithm: This approach uses the update() method to merge the new_dict into the existing dictionary.
- Define the dict_update_method function, which takes in two dictionaries dictionary and new_dict.
- Use the update() method to merge the key-value pairs from new_dict into dictionary.
- Call the dict_update_method function with the two dictionaries to update dictionary with the key-value pairs from new_dict.
- Print out the dictionary before and after the update to verify the update was successful
Python3
def dict_update_method(dictionary, new_dict):
dictionary.update(new_dict)
example_input = { 'a' : 1 , 'b' : 2 }
new_dict = { 'b' : 3 , 'c' : 4 }
initialized_dict = { 'best' : 42 , 'Gfg' : 13 }
print ( "Before update:" , example_input)
dict_update_method(example_input, new_dict)
print ( "After update:" , example_input)
print ( "Initialized dictionary:" , initialized_dict)
|
OutputBefore update: {'a': 1, 'b': 2}
After update: {'a': 1, 'b': 3, 'c': 4}
Initialized dictionary: {'best': 42, 'Gfg': 13}
Time Complexity: O(n) – where n is the number of items in the new_dict.
Auxiliary Space: O(1) – constant space is required for storing the new_dict and the updated dictionary.
Method 4: Use a simple for loop
Python3
from operator import add, mul
helper_fnc = { 'Gfg' : add,
'best' : mul}
x = 6
y = 7
res = {}
for key, func in helper_fnc.items():
res[key] = func(x, y)
print ( "The Initialized dictionary : " + str (res))
|
OutputThe Initialized dictionary : {'Gfg': 13, 'best': 42}
Time complexity: O(n), where n is the number of key-value pairs in the helper_fnc dictionary.
Auxiliary space: O(n), where n is the number of key-value pairs in the helper_fnc dictionary, because we need to create a new dictionary res to store the results.