3

How to Store Values in Dictionary in Python Using For Loop

 7 months ago
source link: https://www.geeksforgeeks.org/how-to-store-values-in-dictionary-in-python-using-for-loop/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to Store Values in Dictionary in Python Using For Loop

Courses
Practice

In this article, we will explore the process of storing values in a dictionary in Python using a for loop. As we know, combining dictionaries with for loops is a potent technique in Python, allowing iteration over keys, values, or both. This discussion delves into the fundamentals of Python dictionaries and provides a demonstration of how to effectively store values within a dictionary using a for loop.

Store Values in Dictionary in Python Using For Loop

Here, are methods for storing values in a Python dictionary using a for loop.

  • Using For Loop
  • Using Zip Function
  • Using Enumerate Function

Store Values in Dictionary in Python Using For Loop

In this example, below Python code below establishes an empty dictionary named my_dict and provides sample data with keys (e.g., ‘name’, ‘age’, ‘city’) and corresponding values (e.g., ‘John’, 25, ‘New York’). Through a for loop, it populates the dictionary by assigning values to their respective keys.

  • Python3
# Initialize an empty dictionary
my_dict = {}
# Sample data (you can replace this with your own data)
keys = ['name', 'age', 'city']
values = ['John', 25, 'New York']
# Using a for loop to populate the dictionary
for i in range(len(keys)):
my_dict[keys[i]] = values[i]
# Print the resulting dictionary
print(my_dict)

Output :

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

Store Values in Dictionary in Python Using zip Function

In this Python example, the zip function is utilized to pair up the elements from the keys and values lists, and a dictionary named my_dict is created using a concise dictionary comprehension. This approach simplifies the process of combining keys and values, resulting in a dictionary with the specified key-value pairs.

  • Python3
#Example
keys = ['name', 'age', 'city']
values = ['John', 25, 'New York']
# Using the zip function to pair up keys 
#and values
my_dict = {key: value for key, 
value in zip(keys, values)}
# Print the resulting dictionary
print(my_dict)

Output :

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

Store Values in Dictionary in Python Using enumerate Function

In this example, below Python code, two lists, `keys` and `values`, are provided. An empty dictionary named `my_dict` is initialized. The for loop, utilizing `enumerate()`, iterates through the keys, pairs them with corresponding values using the index `i`, and populates the dictionary.

  • Python3
keys = ['name', 'age', 'city']
values = ['John', 25, 'New York']
# Initialize an empty dictionary
my_dict = {}
# Using a for loop with enumerate to populate the dictionary
for i, key in enumerate(keys):
my_dict[key] = values[i]
# Print the resulting dictionary
print(my_dict)

Output :

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

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 24 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK