Python – Assign values to initialized dictionary keys
Last Updated :
25 Apr, 2023
Sometimes, while working with python dictionaries, we can have a problem in which we need to initialize dictionary keys with values. We save a mesh of keys to be initialized. This usually happens during web development while working with JSON data. Lets discuss certain ways in which this task can be performed.
Method #1 : Using dict() + zip() The combination of above methods can be used to perform this task. In this, we allot the list values to already constructed mesh and zip() helps in mapping values as per list index.
Python3
test_dict = { 'gfg' : ' ', ' is ': ' ', ' best ': ' '}
test_list = [ 'A' , 'B' , 'C' ]
print ( "The original dictionary is : " + str (test_dict))
res = dict ( zip (test_dict, test_list))
print ( "The assigned value dictionary is : " + str (res))
|
Output :
The original dictionary is : {'is': '', 'best': '', 'gfg': ''}
The assigned value dictionary is : {'gfg': 'C', 'best': 'B', 'is': 'A'}
Time Complexity: O(n), where n is the number of elements in the dictionary.
Auxiliary Space: O(n), as we are using a dictionary and a list to store the values.
Method #2 : Using loop + zip() This is extended way in which this task can be performed. In this, we iterate through the zipped list and assign value to dictionary.
Python3
test_dict = { 'gfg' : ' ', ' is ': ' ', ' best ': ' '}
test_list = [ 'A' , 'B' , 'C' ]
print ( "The original dictionary is : " + str (test_dict))
for key, val in zip (test_dict, test_list):
test_dict[key] = val
print ( "The assigned value dictionary is : " + str (test_dict))
|
Output :
The original dictionary is : {'is': '', 'best': '', 'gfg': ''}
The assigned value dictionary is : {'gfg': 'C', 'best': 'B', 'is': 'A'}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using keys() and for loop
Python3
test_dict = { 'gfg' : ' ', ' is ': ' ', ' best ': ' '}
test_list = [ 'A' , 'B' , 'C' ]
print ( "The original dictionary is : " + str (test_dict))
x = list (test_dict.keys())
for i in range ( 0 , len (x)):
test_dict[x[i]] = test_list[i]
print ( "The assigned value dictionary is : " + str (test_dict))
|
Output
The original dictionary is : {'gfg': '', 'is': '', 'best': ''}
The assigned value dictionary is : {'gfg': 'A', 'is': 'B', 'best': 'C'}
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #4: Using dictionary comprehension
Use dictionary comprehension to create a new dictionary where the keys are the same as the original dictionary and the values are taken from the test_list. The enumerate() function is used to get the index of each key in the original dictionary, which is used to get the corresponding value from the test_list.
- Initialize a dictionary with some keys and empty values.
- Create a list of values that need to be assigned to the keys of the dictionary.
- Print the original dictionary to verify the initial state.
- Use any of the provided methods (or the new method) to assign the values to the keys of the dictionary.
- Print the updated dictionary to verify the assigned values.
Python3
test_dict = { 'gfg' : ' ', ' is ': ' ', ' best ': ' '}
test_list = [ 'A' , 'B' , 'C' ]
print ( "The original dictionary is : " + str (test_dict))
test_dict = {key: test_list[i] for i, key in enumerate (test_dict)}
print ( "The assigned value dictionary is : " + str (test_dict))
|
Output
The original dictionary is : {'gfg': '', 'is': '', 'best': ''}
The assigned value dictionary is : {'gfg': 'A', 'is': 'B', 'best': 'C'}
Time complexity: O(n), where n is the number of keys in the dictionary
Auxiliary space: O(n), where n is the number of keys in the dictionary
Method #5: Using dictionary.update() method
Steps:
- Initialize the dictionary test_dict with 3 keys and empty values.
- Initialize the list test_list with 3 elements.
- Print the original dictionary test_dict.
- Loop through the keys in test_dict using enumerate() to get the index i and the key itself key.
- Update the value of the key in test_dict using update() method with a new dictionary containing only the key-value pair key: test_list[i].
- Print the resulting dictionary test_dict with assigned values.
Python3
test_dict = { 'gfg' : ' ', ' is ': ' ', ' best ': ' '}
test_list = [ 'A' , 'B' , 'C' ]
print ( "The original dictionary is : " + str (test_dict))
for i, key in enumerate (test_dict):
test_dict.update({key: test_list[i]})
print ( "The assigned value dictionary is : " + str (test_dict))
|
Output
The original dictionary is : {'gfg': '', 'is': '', 'best': ''}
The assigned value dictionary is : {'gfg': 'A', 'is': 'B', 'best': 'C'}
Time complexity: O(n), where n is the number of keys in the dictionary
Auxiliary space: O(n), where n is the number of keys in the dictionary
Share your thoughts in the comments
Please Login to comment...