Open In App

Create Dynamic Dictionary Python using for Loop

Last Updated : 31 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A for loop may be used to cycle over a collection of keys and values in Python to generate a dynamic dictionary. With this method, we may create dictionaries on the fly depending on specific requirements or information. In this article, we will see how to create a dynamic dictionary in Python using a for loop.

Python Create a Dynamic Dictionary Using For Loop

Below are some examples by which we can create a dynamic dictionary using for loop in Python:

Dynamic Dictionary Using a Simple for Loop

This method assigns each key-value pair as iterates over the lists of keys and values, so dynamically creating a dictionary. It does this by using a simple for loop.

Python3




# Approach 1: Using a Simple For Loop
keys = ['name', 'age', 'city']
values = ['Rahul', 25, 'New York']
 
dynamic_dict = {}
for i in range(len(keys)):
    dynamic_dict[keys[i]] = values[i]
 
print(dynamic_dict)


Output

{'name': 'Rahul', 'age': 25, 'city': 'New York'}



Python Dynamic Dictionary Using zip() and for Loop

The for loop iterates over the pairs of matching entries from the keys and values lists that the zip() function couples, resulting in the creation of a dynamic dictionary.

Python3




# Approach 2: Using Zip and For Loop
keys = ['name', 'age', 'city']
values = ['Rahul', 25, 'New York']
 
dynamic_dict = {}
for key, value in zip(keys, values):
    dynamic_dict[key] = value
 
print(dynamic_dict)


Output

{'name': 'Rahul', 'age': 25, 'city': 'New York'}



Create Dynamic Dictionary Using Dictionary Comprehension

We may iterate over the keys and indices at the same time by combining enumerate and dictionary comprehension to create the dynamic dictionary.

Python3




# Approach 3: Dictionary Comprehension with Enumerate
keys = ['name', 'age', 'city']
values = ['Rahul', 25, 'New York']
 
dynamic_dict = {key: values[i] for i, key in enumerate(keys)}
 
print(dynamic_dict)


Output

{'name': 'Rahul', 'age': 25, 'city': 'New York'}



Dynamic Dictionary Using Items() Method

By sending the view of dictionary key-value pairs that the items() function returns to the dict constructor, we are able to build a dynamic dictionary.

Python3




#Approach 4: Using Items() Method
keys = ['name', 'age', 'city']
values = ['Rahul', 25, 'New York']
 
dynamic_dict = dict(zip(keys, values))
 
print(dynamic_dict)


Output

{'name': 'Rahul', 'age': 25, 'city': 'New York'}



Python Dynamic Dictionary by Merging Dictionaries in a For Loop

We may combine key-value pairs into the dictionary dynamically by using the update() function inside of a for loop

Python3




# Approach 5: Merging Dictionaries in a For Loop
keys = ['name', 'age', 'city']
values = ['Rahul', 25, 'New York']
 
dynamic_dict = {}
for key, value in zip(keys, values):
    dynamic_dict.update({key: value})
 
print(dynamic_dict)


Output

{'name': 'Rahul', 'age': 25, 'city': 'New York'}





Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads