The Wayback Machine - https://web.archive.org/web/20230201023145/https://www.geeksforgeeks.org/python-assign-values-to-initialized-dictionary-keys/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Assign values to initialized dictionary keys

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 06 Dec, 2022
Improve Article
Save Article

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'}

  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'}

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'}

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!