Creating a new dictionary 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 is one of the first things Python developers learn because dictionaries are used everywhere, from configuration data to fast lookups and counters. Python gives you several ways to build one, and the best choice depends on whether your keys are known in advance, generated dynamically, or coming from existing data.
Start with Literal Syntax
If you already know the keys and values, dictionary literal syntax is usually the clearest option.
This style is readable and direct. It is the default choice for static mappings written by hand.
Use dict() When the Input Shape Fits It
The dict constructor is useful in two common cases. The first is keyword-style creation:
This is compact, but it works only when the keys are valid Python identifiers. For keys such as "max-retries" or "user name", use literal syntax or iterable conversion instead.
The second common use is converting key-value pairs into a dictionary:
This is handy when data already exists as tuples, zipped columns, or parsed records.
Dictionary Comprehensions for Generated Data
When keys and values are derived from a pattern, a dictionary comprehension is often the cleanest solution.
Comprehensions are concise, but do not force complicated logic into one line just because you can. If the expression becomes hard to read, a regular loop is better.
Build Incrementally in a Loop
For multi-step logic, create an empty dictionary and fill it as you go.
This pattern is extremely common because it stays readable even when the update logic becomes more complex than a simple comprehension can handle.
fromkeys() and Shared Defaults
dict.fromkeys() creates a dictionary with the same default value for every key.
That is fine for immutable defaults such as numbers or strings. With mutable defaults, however, every key shares the same underlying object.
Both keys now point to the same list. That surprises people constantly, so avoid fromkeys with mutable defaults unless shared state is exactly what you want.
Nested Dictionaries and Practical Patterns
Real-world data is often nested, and dictionaries make that natural:
If you need to build nested mappings dynamically, defaultdict can reduce boilerplate:
That can be more convenient than repeatedly checking whether intermediate keys already exist.
Copying and Merging Dictionaries
Creating a "new" dictionary often means copying or combining existing ones.
In modern Python, the merge operator is also available:
This creates a new dictionary rather than mutating the original base, which is often the safer behavior in larger codebases.
Common Pitfalls
One common mistake is using square-bracket access on a key that might not exist. If missing keys are expected, get or setdefault can be safer than catching KeyError repeatedly.
Another is overusing clever comprehensions where a normal loop would be easier to maintain. Brevity is not always clarity.
The biggest practical trap is shared mutable defaults from fromkeys. That bug can hide for a long time before someone notices that multiple keys are changing together.
Finally, remember that dictionary keys should be hashable and stable. Mutable types such as lists cannot be used as keys.
Summary
- Use literal syntax when keys and values are known directly.
- Use
dict()for keyword arguments or iterables of key-value pairs. - Use comprehensions for simple generated mappings.
- Use loops when the construction logic is more complex.
- Be careful with
fromkeys()when the default value is mutable.

