Recently, while working on a natural language processing project, I needed to count word frequencies in a large text corpus. The issue was, I needed to initialize a dictionary with zero values for all potential words before counting. Python dictionaries are perfect for this task, but there’s no single built-in method to initialize all keys with zeros.
In this article, I’ll cover five simple yet effective ways to initialize a Python dictionary with zeros. These methods will work whether you’re counting word frequencies, tracking inventory, or storing any kind of numerical data that starts from zero.
So let’s dive in!
Initialize Dictionary Python with 0
Now, I am going to explain to you how to initialize a dictionary in Python with 0.
Read Python Dictionary Comprehension
Method 1 – Dictionary Comprehension with Zero Values
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)Output:
{'California': 0, 'Texas': 0, 'Florida': 0, 'New York': 0, 'Pennsylvania': 0}I executed the above example code and added the screenshot.

This method is concise and readable. It creates a dictionary where each key from our list is assigned a value of 0. I use this approach frequently when I already have a list of keys ready to go.
Check out Python Dictionary Key Error
Method 2 – dict.fromkeys() Method
Python’s built-in dict.fromkeys() method provides a clean way to create a dictionary with default values.
# Initialize dictionary with keys from a list and all values as 0
stock_tickers = ['AAPL', 'MSFT', 'AMZN', 'GOOGL', 'META']
daily_price_change = dict.fromkeys(stock_tickers, 0)
print(daily_price_change)Output:
{'AAPL': 0, 'MSFT': 0, 'AMZN': 0, 'GOOGL': 0, 'META': 0}I executed the above example code and added the screenshot.

This method is slightly more efficient than dictionary comprehension and accomplishes the same result. I find it particularly useful when I want a cleaner syntax.
Read Python Dictionary KeyError: None
Method 3 – Use For Loop with a Pre-defined Dictionary
Sometimes, you might need more control over the initialization process. Python for loop gives you that flexibility.
# Initialize empty dictionary first
website_visitors = {}
# Add keys with 0 values using a loop
domains = ['homepage', 'about', 'products', 'contact', 'blog']
for domain in domains:
website_visitors[domain] = 0
print(website_visitors)Output:
{'homepage': 0, 'about': 0, 'products': 0, 'contact': 0, 'blog': 0}I executed the above example code and added the screenshot.

This approach might be slightly more verbose, but it gives you more control and allows you to add conditional logic during initialization if needed.
Check out Remove a Key Without an Error in the Python Dictionary
Method 4 – Use defaultdict from collections
The defaultdict class from the collections module automatically initializes new keys with a default value when accessed.
from collections import defaultdict
# Create a defaultdict with default value 0
city_temperatures = defaultdict(int) # int() returns 0 by default
# Adding some cities
cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']
for city in cities:
# No need to initialize - defaultdict handles it
city_temperatures[city] # Automatically set to 0
print(dict(city_temperatures))Output:
{'New York': 0, 'Los Angeles': 0, 'Chicago': 0, 'Houston': 0, 'Phoenix': 0}The beauty of defaultdict is that it automatically assigns the default value (0 in this case) whenever a new key is accessed. This is incredibly useful for counting and accumulating values.
Read KeyError in a Nested Python Dictionary
Method 5 – Use Counter with Initial Values
If you’re specifically working with counting scenarios, the Counter class from collections can be initialized with zeros.
from collections import Counter
nba_teams = ['Lakers', 'Celtics', 'Warriors', 'Bulls', 'Heat']
team_wins = Counter({team: 0 for team in nba_teams})
print(team_wins)Output:
Counter({'Lakers': 0, 'Celtics': 0, 'Warriors': 0, 'Bulls': 0, 'Heat': 0})Counter is specialized for counting scenarios and offers additional methods like most_common() that can be useful when you start incrementing these zero values.
Check out Python Dictionary Comprehension
Real-World Application: Word Frequency Counter
Let’s see a practical example of initializing a Python dictionary with zeros for word frequency counting:
# Sample text from a US news headline dataset
text = "The Federal Reserve has raised interest rates to combat inflation in the United States economy. Economists predict further rate adjustments may occur in the coming months."
# Initialize an empty dictionary to store word frequencies
word_counts = {}
# Tokenize the text into words and convert to lowercase
words = text.lower().split()
# Initialize dictionary with 0 for each unique word
unique_words = set(words)
word_counts = {word: 0 for word in unique_words}
# Count word frequencies
for word in words:
word_counts[word] += 1
print(word_counts)Output:
{'the': 4, 'federal': 1, 'reserve': 1, 'has': 1, 'raised': 1, 'interest': 1, 'rates': 1, 'to': 1, 'combat': 1, 'inflation': 1, 'in': 2, 'united': 1, 'states': 1, 'economy.': 1, 'economists': 1, 'predict': 1, 'further': 1, 'rate': 1, 'adjustments': 1, 'may': 1, 'occur': 1, 'coming': 1, 'months.': 1}In this example, we first initialize the dictionary with zeros for each unique word, then increment the counts as we process the text. This pattern is extremely common in data analysis and text processing workflows.
Read Check if Python Dictionary is Empty
Choose the Right Method
The best method to initialize a dictionary with zeros depends on your specific needs:
- If you want clean, readable code: Use dictionary comprehension (Method 1)
- If you want slightly better performance, use dict.fromkeys() (Method 2)
- If you need flexibility during initialization, use a for loop (Method 3)
- If you need automatic initialization when accessing new keys, use defaultdict (Method 4)
- If you’re specifically counting items: Use Counter (Method 5)
I personally use dictionary comprehension most frequently for its readability, but I switch to defaultdict when I need to build the dictionary incrementally without knowing all keys upfront.
I hope you found this article helpful for working with dictionaries in Python. Each of these methods has its advantages, and knowing them all will make you a more versatile Python programmer.
If you have any questions or suggestions for me, please let me know in the comments below.
Other Python articles you may also like:
- Read a CSV into a Dictionary using Pandas in Python
- Python Dictionary of Sets
- Copy a Dictionary in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.