How to initialize a dict with keys from a list and empty value in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Creating a dictionary from a list of keys with a default value is one of the most common dictionary operations in Python. Whether you are initializing counters, building lookup tables, or preparing data structures for later population, knowing the right approach matters. This article covers the main techniques, explains a critical trap that catches even experienced developers, and shows you when to use each method.
Using dict.fromkeys()
The built-in dict.fromkeys() class method is the most concise way to create a dictionary from a list of keys, all set to the same default value.
This method is clean, readable, and runs in O(n) time where n is the number of keys. However, it has a dangerous trap when used with mutable defaults.
The Mutable Default Trap
This is the single most important thing to understand about dict.fromkeys(). When you pass a mutable object like a list or dictionary as the default value, every key shares a reference to the same object. Modifying one modifies them all.
This happens because fromkeys() evaluates the default value once and assigns that single reference to every key. It does not create a new list for each key. If you need mutable defaults, use a dictionary comprehension instead.
Using Dictionary Comprehension
A dictionary comprehension creates a fresh value for each key, which makes it the safe choice when your default value is mutable.
The comprehension evaluates the value expression on each iteration, so every key gets an independent object. This is the method you should reach for by default whenever mutable values are involved.
Using collections.defaultdict
If you do not know all the keys upfront, or you want values to be created lazily on first access, defaultdict from the collections module is the right tool.
The key advantage of defaultdict is that you never need to check whether a key exists before using it. The factory function is called automatically for missing keys, which simplifies code that accumulates values.
Comparing the Approaches
Here is a practical comparison to help you choose.
Use dict.fromkeys() for immutable defaults when you want clean, readable code. Use dictionary comprehensions when the default value is mutable. Use defaultdict when keys are discovered dynamically during program execution.
Common Pitfalls
- Using
dict.fromkeys()with a mutable default causes all keys to share the same object. Always use a comprehension for lists, sets, or dicts as values. - Passing
Nonetofromkeysand forgetting to handle it leads toTypeErrorlater when you try to operate on theNonevalues without checking first. - Using
defaultdictwhen you needKeyErrorbehavior silently creates entries for typos and missing keys. Use a regular dict if you want access to non-existent keys to fail loudly. - Forgetting that
dict.fromkeys()deduplicates keys means if your list contains duplicates, the resulting dictionary will have fewer entries than expected, since dictionaries cannot have duplicate keys. - Confusing
{}.fromkeys()withdict.fromkeys()produces the same result, butdict.fromkeys()is more readable and conventional. Calling it on an instance is misleading because the instance's existing contents are ignored.
Summary
dict.fromkeys(keys, default)is the fastest one-liner for immutable defaults like0,"",None, orFalse.- Dictionary comprehensions (
{k: value for k in keys}) create independent value objects per key, making them safe for mutable defaults. defaultdict(factory)lazily creates values on first access, ideal when you do not know all keys ahead of time.- Never use
dict.fromkeys()with a mutable default value. This is the most common source of bugs with this pattern. - Choose the method based on whether your default is mutable and whether your keys are known at creation time.

