You can use a dictionary comprehension (supported in Python 2.7+):

>>> animals = ["dog", "cat", "cow"]
>>> {x: {} for x in animals}
{'dog': {}, 'cow': {}, 'cat': {}}
Answer from Eugene Yarmash on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί how-to-initialize-a-dictionary-in-python-using-for-loop
How to Initialize a Dictionary in Python Using For Loop - GeeksforGeeks
July 23, 2025 - In this example, we can initialize a dictionary using the for loop with the help of main iteration of an older existing iterable like even the list that has the tuples with key-value pairs.
Discussions

python - Initializing a dictionnary with a for loop - Stack Overflow
I would like to initialize my dictionary res with empty vectors 'W1':[], 'W2':[] up to 100, but I don't know how to iterate on the i value. More on stackoverflow.com
🌐 stackoverflow.com
python - Initialize List to a variable in a Dictionary inside a loop - Stack Overflow
I have been working for a while in Python and I have solved this issue using "try" and "except", but I was wondering if there is another method to solve it. Basically I want to create a dictionar... More on stackoverflow.com
🌐 stackoverflow.com
April 1, 2014
Adding Key: Value to Empty Dictionary via For Loop
dictionary keys are "unique" - so you are overwriting the previous entry each time. >>> quotes = {} >>> quotes['author'] = 'quote1' >>> quotes {'author': 'quote1'} >>> quotes['author'] = 'quote2' >>> quotes {'author': 'quote2'} Perhaps you want a list of dicts? [ { 'author': 'quote1' }, { 'author': 'quote2' }, ... ] More on reddit.com
🌐 r/learnpython
6
1
December 3, 2022
How to append to dictionary within a for loop
# Adding the entry {'Team_A' : 10} to a dict my_dict = {} my_dict['Team_A'] = 10 # Retrieving the value from a dict team_a_score = my_dict['Team_A'] print(team_a_score) # prints 10 ---------------------------------- He's a simplified version of what you currently have names_list = ['person_A', 'person_B', 'person_C'] d = {} for name in names_list: d[name].append(10) The piece d[name] on the last line is how you would retrieve a value from a dictionary. It tries to retrieve the first name in the list, person_A, from the dictionary and throws a KeyError because that key doesn't exist. Did you mean to instead add a value to the dictionary? You would use the assignment operator, = # assigns the value 10 to eat name key names_list = ['person_A', 'person_B', 'person_C'] d = {} for name in names_list: d[name] = 10 More on reddit.com
🌐 r/learnpython
5
3
July 24, 2022
🌐
Reddit
reddit.com β€Ί r/learnprogramming β€Ί python dictionary initialization using for loop
r/learnprogramming on Reddit: Python Dictionary Initialization using For Loop
May 15, 2022 -

I am working on a Python program to analyze hockey data and am trying to build it in such a way that I can add additional parameters to analyze later on. I thought that a for loop would be a good way to do this:

seasons = ['current', 'last']
event_types = ['shot', 'goal']
league_data = {}
for season in seasons:
    for event in event_types:
        league_data[season][event] = {}
        league_data[season][event]['x'] = []
        league_data[season][event]['y'] = []

When I run the code I get a KeyError (KeyError: 'current'). Is it possible to initialize dictionaries using for loops? Or should I simply declare all of the dictionaries explicitly and save myself the headache?

EDIT:

I think I've figured it out. I looped through and created the league_data[season] entries first and then added the event entries in a separate loop.

🌐
Stack Overflow
stackoverflow.com β€Ί questions β€Ί 69659511 β€Ί initializing-a-dictionnary-with-a-for-loop
python - Initializing a dictionnary with a for loop - Stack Overflow
I would like to initialize my dictionary res with empty vectors 'W1':[], 'W2':[] up to 100, but I don't know how to iterate on the i value. res = {'lambda' : []} for i in range(0,99): res.update('W...
🌐
W3Schools
w3schools.com β€Ί python β€Ί python_dictionaries_loop.asp
Python - Loop Dictionaries
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... You can loop through a dictionary by using a for loop.
🌐
Coding Rooms
codingrooms.com β€Ί blog β€Ί dictionary-with-for-loop-python
Python Dictionary with For Loop
October 7, 2020 - List Comprehension is tough at first, because it feels unnatural, but the more you code in python, the more you will find the added benefits of using list comprehension. Just remember: Everything you do with list comprehension can be done with a for loop. But the inverse is not true. ... So how does this apply to the above problem? Well, below I show how we can use this new format to assist us! # Initialize the dictionary fruits = {'banana':3,'apple':2, 'mango':1, 'kiwi':5} # Create blank list to append to fruits_list = [[fruit]*quantity for fruit, quantity in fruits.items()] # Print out the final list print(fruits_list)
🌐
Python Guides
pythonguides.com β€Ί python-dictionary-initialize
How To Initialize Dictionary Python With 0
May 16, 2025 - In Python, a common way to initialize a dictionary with default zero values is through comprehension Β· # Initialize dictionary with keys from a list and all values as 0 us_states = ['California', 'Texas', 'Florida', 'New York', 'Pennsylvania'] state_population_growth = {state: 0 for state in us_states} print(state_population_growth)
Find elsewhere
🌐
SheCodes
shecodes.io β€Ί athena β€Ί 73172-creating-a-dictionary-using-a-for-loop-in-python
[Python] - Creating a Dictionary Using a For Loop in Python | SheCodes
Consider the following Python program. fin = open('words.txt') for line in fin: word = line.strip() print(word) What does the program loop over?
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί create-dynamic-dictionary-python-using-for-loop
Create Dynamic Dictionary using for Loop-Python - GeeksforGeeks
July 23, 2025 - When combined with a for loop, it enables us to iterate over two lists simultaneously and creating key-value pairs to dynamically build a dictionary. This method is efficient, eliminating the need for indexing and making it a more Pythonic solution ...
🌐
Python Examples
pythonexamples.org β€Ί python-dictionary-loop-example
How to Loop through Dictionary in Python?
To loop through a dictionary, we can use Python for loop. In this tutorial, we will learn how to iterate through key:value pairs of dictionary, or just the keys or just the values. In this example, we will initialize a dictionary with three key:value pairs.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python-dictionary-initialization-with-common-dictionary
Python | Dictionary initialization with common dictionary - GeeksforGeeks
April 10, 2023 - Use a for loop to iterate over the range of integers from 0 to 3: a. For each integer i in the range, create a new key-value pair in res where the key is i and the value is a copy of test_dict.
🌐
AskPython
askpython.com β€Ί home β€Ί how to create a nested dictionary via for loop?
How to Create a Nested Dictionary via for Loop? - AskPython
March 25, 2023 - In the next line, we are initializing a for loop that runs through every number till n. So if n is given as 5, the code will execute five times. In the loop, we are using a print function that prints the square of the number in the range of n. ... Let us now look at a few examples of creating a nested dictionary using for loop.
🌐
AskPython
askpython.com β€Ί home β€Ί how to initialize dictionary in python – a step-by-step guide
How to Initialize Dictionary in Python - A Step-By-Step Guide - AskPython
September 24, 2022 - Initializing Dictionary Using For Loop Instead Of fromkeys() Method Β· We can use setdefault() method to initialize dictionary in python as follows.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί adding-items-to-a-dictionary-in-a-loop-in-python
Adding Items to a Dictionary in a Loop in Python - GeeksforGeeks
January 23, 2025 - This method initializes a dictionary with predefined keys and default value which can then be updated with a loop. Although this method is less efficient for cases where both keys and values are available upfront, it still serves as a valid approach for dictionary initialization.
🌐
UltaHost
ultahost.com β€Ί knowledge-base β€Ί initialize-dictionary-python
How to Initialize Dictionary in Python | Ultahost Knowledge Base
March 13, 2025 - Learn how to initialize dictionaries in Python using methods like dict(), comprehension, fromkeys(), and more. Explore best practices.
🌐
Real Python
realpython.com β€Ί iterate-through-dictionary-python
How to Iterate Through a Dictionary in Python – Real Python
November 23, 2024 - You can use this view object to iterate through the dictionary keys. To do this, call .keys() in the header of a for loop: ... When you call .keys() on likes, you get a view of keys. Python knows that view objects are iterable, so it starts looping.
🌐
Developmentality
developmentality.wordpress.com β€Ί 2012 β€Ί 03 β€Ί 30 β€Ί three-ways-of-creating-dictionaries-in-python
Three ways of creating dictionaries in Python | Developmentality
February 12, 2014 - I don’t understand. Yes you can create a dictionary using a loop. Something like: >>> d = {} >>> for k, v in [(β€œk1”, β€œv1”), (β€œk2”, β€œv2”)]: …
🌐
CodeRivers
coderivers.org β€Ί blog β€Ί python-initialise-dictionary
Python Dictionary Initialization: A Comprehensive Guide - CodeRivers
February 22, 2026 - You can also use a loop to initialize ... the default values based on some conditions. keys = ['x', 'y', 'z'] new_dict = {} for key in keys: new_dict[key] = key.upper() print(new_dict) # Output: {'x': 'X', 'y': 'Y', 'z': 'Z'}...
🌐
Python Guides
pythonguides.com β€Ί create-a-dictionary-in-python-using-a-for-loop
How To Create A Dictionary In Python Using A For Loop?
March 19, 2025 - We then use a for loop to iterate over each name in the names list. Inside the loop, we add a new key-value pair to the name_lengths dictionary, where the key is the name and the value is the length of the name obtained using the len() function. Check out Write a Python Program to Remove Duplicates From a Dictionary