Open In App
Related Articles

Python – Assign values to initialized dictionary keys

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

# Python3 code to demonstrate working of
# Assign values to initialized dictionary keys
# using dict() + zip()
 
# initializing dictionary
test_dict = {'gfg': '', 'is': '', 'best': ''}
 
# Initializing list
test_list = ['A', 'B', 'C']
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Assign values to initialized dictionary keys
# using dict() + zip()
res = dict(zip(test_dict, test_list))
 
# printing result
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

# Python3 code to demonstrate working of
# Assign values to initialized dictionary keys
# using loop + zip()
 
# initializing dictionary
test_dict = {'gfg': '', 'is': '', 'best': ''}
 
# Initializing list
test_list = ['A', 'B', 'C']
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Assign values to initialized dictionary keys
# using loop + zip()
for key, val in zip(test_dict, test_list):
    test_dict[key] = val
 
# printing result
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

# Python3 code to demonstrate working of
# Assign values to initialized dictionary keys
 
# initializing dictionary
test_dict = {'gfg': '', 'is': '', 'best': ''}
 
# Initializing list
test_list = ['A', 'B', 'C']
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Assign values to initialized dictionary keys
x = list(test_dict.keys())
for i in range(0, len(x)):
    test_dict[x[i]] = test_list[i]
# printing result
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

# Python3 code to demonstrate working of
# Assign values to initialized dictionary keys
 
# initializing dictionary
test_dict = {'gfg': '', 'is': '', 'best': ''}
 
# Initializing list
test_list = ['A', 'B', 'C']
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Assign values to initialized dictionary keys
test_dict = {key: test_list[i] for i, key in enumerate(test_dict)}
 
# printing result
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

# Python3 code to demonstrate working of
# Assign values to initialized dictionary keys
 
# initializing dictionary
test_dict = {'gfg': '', 'is': '', 'best': ''}
 
# Initializing list
test_list = ['A', 'B', 'C']
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Assign values to initialized dictionary keys using update() method
for i, key in enumerate(test_dict):
    test_dict.update({key: test_list[i]})
 
# printing result
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



Last Updated : 25 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads