Initialize a Python Dictionary with Default Values

I was working on a project where I had to initialize a Python dictionary with default values for multiple keys.

At first, I thought there would be just one easy way to do it. But as I explored deeper, I realized Python offers several clean and practical options.

In this tutorial, I’ll share the exact methods I use to initialize dictionaries with default values. I’ll also explain when each approach makes sense, so you can pick the best one for your use case.

Methods to Initialize a Python Dictionary with Default Values

When working with real-world data, it’s common to prepare a dictionary where all keys should start with the same initial value.

For example, if I’m tracking sales data across U.S. states, I might want every state to start with a default sales count of 0.

By initializing with default values, I avoid writing repetitive code and reduce the chance of errors later.

1 – Use Python’s dict.fromkeys()

The first method I often use is dict.fromkeys(). It’s a built-in Python method that lets you create a dictionary from a list of keys, all sharing the same default value.

Here’s how I use it:

# Initialize dictionary with default values using dict.fromkeys()
states = ["California", "Texas", "Florida", "New York", "Illinois"]

# Every state starts with sales = 0
sales_data = dict.fromkeys(states, 0)

print(sales_data)

When I run this code, I get:

{'California': 0, 'Texas': 0, 'Florida': 0, 'New York': 0, 'Illinois': 0}

I executed the above example code and added the screenshot below.

python initialize dictionary

This method is perfect when I know all my keys upfront and want the same default value for each.

2 – Use Python’s Dictionary Comprehension

Another approach I love is dictionary comprehension. It’s more flexible than fromkeys() because I can apply logic while creating the dictionary.

Here’s an example:

# Initialize dictionary with default values using dictionary comprehension
states = ["California", "Texas", "Florida", "New York", "Illinois"]

# Assign default sales = 0 using comprehension
sales_data = {state: 0 for state in states}

print(sales_data)

Output:

{'California': 0, 'Texas': 0, 'Florida': 0, 'New York': 0, 'Illinois': 0}

I executed the above example code and added the screenshot below.

initialize dictionary python

This method is my go-to when I need more control over how values are initialized.

3 – Use a For Loop in Python

Sometimes, I prefer writing things step by step for clarity. Using a for loop is the most explicit way to initialize a dictionary.

Here’s how I do it:

# Initialize dictionary with default values using a for loop
states = ["California", "Texas", "Florida", "New York", "Illinois"]

sales_data = {}
for state in states:
    sales_data[state] = 0

print(sales_data)

Output:

{'California': 0, 'Texas': 0, 'Florida': 0, 'New York': 0, 'Illinois': 0}

I executed the above example code and added the screenshot below.

python initialize dict

This method is not as concise, but it’s beginner-friendly and easier to debug.

4 – Use defaultdict from collections in Python

When I’m working with dynamic keys (where I don’t know all the keys upfront), I rely on defaultdict. This class automatically assigns a default value whenever I access a missing key.

Here’s an example:

from collections import defaultdict

# Initialize dictionary with default values using defaultdict
sales_data = defaultdict(int)

# Add values dynamically
sales_data["California"] += 10
sales_data["Texas"] += 5

print(sales_data)

Output:

defaultdict(<class 'int'>, {'California': 10, 'Texas': 5})

I executed the above example code and added the screenshot below.

python init dict

This is extremely useful when I’m counting items or grouping data without worrying about KeyError.

5 – Use dict() Constructor with Tuples

Another neat method is to use the dict() constructor with a list of tuples.

Here’s how it works:

# Initialize dictionary with default values using dict() constructor
states = ["California", "Texas", "Florida", "New York", "Illinois"]

sales_data = dict((state, 0) for state in states)

print(sales_data)

Output:

{'California': 0, 'Texas': 0, 'Florida': 0, 'New York': 0, 'Illinois': 0}

This approach is similar to comprehension but uses the constructor style, which some developers prefer for readability.

Practical Example – U.S. Election Votes Tracker

To make this more practical, let me show you how I used these methods in a project.

Suppose I’m building a system to track electoral votes across U.S. states. Initially, every state should start with 0 votes.

Here’s how I can set it up:

# Example: Initialize dictionary with default values for election votes
states = ["California", "Texas", "Florida", "New York", "Illinois"]

# Initialize with 0 votes
votes = {state: 0 for state in states}

# Update votes dynamically
votes["California"] += 55
votes["Texas"] += 38

print(votes)

Output:

{'California': 55, 'Texas': 38, 'Florida': 0, 'New York': 0, 'Illinois': 0}

This way, I can start with clean default values and update them as real data comes in.

Things to Keep in Mind

  • If you use dict.fromkeys() with a mutable default value (like a list), all keys will share the same reference.
  • defaultdict is great for dynamic keys, but returns a specialized object, not a plain dictionary.
  • Dictionary comprehension is the most Pythonic and flexible option in most cases.

Conclusion

While Python doesn’t force you into one way of initializing dictionaries with default values, it gives you multiple clean options.

I personally use dictionary comprehension the most, but defaultdict is my favorite when handling dynamic data.

Other Python tutorials you may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.