Skip to main content

How to Create a Nested Dictionary in Python

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

One dictionary can describe one thing: a tea with its price and its size. To describe several things, each with its own details, put a dictionary inside a dictionary. This guide creates such a nested dictionary on Python 3.15.2, adds an entry to it and changes a value inside it.

Syntax​

Syntax
<dictionary> = {
<key>: {<inner_key>: <value>, <inner_key>: <value>},
<key>: {<inner_key>: <value>, <inner_key>: <value>},
}

<dictionary>[<new_key>] = {<inner_key>: <value>}
<dictionary>[<key>][<inner_key>] = <new_value>
PartWhat it means
<key>A key of the outer dictionary, such as an item's name
{<inner_key>: <value>, ...}The value stored under that key: a whole dictionary of its own
<dictionary>[<new_key>] = {...}Adds one more inner dictionary under a new key
<dictionary>[<key>][<inner_key>] = <new_value>Changes one value inside an inner dictionary. The first brackets pick the inner dictionary, the second the key in it

A dictionary's values can be anything, another dictionary included. The built-in types reference says: "A mapping object maps hashable values to arbitrary objects." The keys are the hashable values, and the values are the arbitrary objects.

The Example​

A café menu has two items, tea and cake. Each has a price and a size. The script creates the menu, adds a juice, and prints the whole menu and the entry for tea.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

menu.py
menu = {
"tea": {"price": 2.5, "size": "cup"},
"cake": {"price": 4.0, "size": "slice"},
}

menu["juice"] = {"price": 3.0, "size": "glass"}

print(menu)
print(menu["tea"])

The menu.py script in a macOS-style code window: a menu dictionary whose tea and cake keys each hold a dictionary with a price and a size, a line that adds juice with its own dictionary, then prints of the menu and of menu["tea"].

The outer dictionary is written over several lines, one item per line, so each inner dictionary is easy to read. The comma after the last item is allowed and makes it simple to add a line later.

Step 2: Run It​

Terminal
uv run python menu.py
Output
{'tea': {'price': 2.5, 'size': 'cup'}, 'cake': {'price': 4.0, 'size': 'slice'}, 'juice': {'price': 3.0, 'size': 'glass'}}
{'price': 2.5, 'size': 'cup'}

The terminal after uv run python menu.py: the whole menu with tea, cake and juice, each with its own price and size, then the inner dictionary of tea with price 2.5 and size cup.

The first line is the whole menu: three keys, each holding a dictionary. juice is there too, added after the menu was created. The second line is what menu["tea"] holds: the inner dictionary for tea on its own. The command works unchanged in PowerShell.

Change a Value Inside​

To change the price of tea, use two pairs of brackets: menu["tea"]["price"] = 2.8. After that line, menu["tea"] printed {'price': 2.8, 'size': 'cup'}. The size stayed, because only the price key was set.

Assigning to menu["tea"] alone replaces the whole inner dictionary. After menu["tea"] = {"price": 3.0}, menu["tea"] printed {'price': 3.0}, and the size was gone.

Common Mistakes​

MistakeWhat happensDo this instead
Setting an inner value before the inner dictionary existsWith an empty menu, the line menu["tea"]["price"] = 2.5 raised KeyError: 'tea'Create the inner dictionary first: menu["tea"] = {}, then menu["tea"]["price"] = 2.5
Using one inner dictionary for two keysWith plain = {"price": 0, "size": "cup"} stored under both tea and coffee, setting the price of tea to 2.5 changed the coffee's price to 2.5 as wellWrite a separate inner dictionary for each key. Then only tea's price changed
Using a dictionary as a key{{"hot": True}: "tea"} raised TypeError: cannot use 'dict' as a dict key (unhashable type: 'dict')Dictionaries can be values, not keys. Use a string or a number as the key

FAQ​

Can I build a nested dictionary step by step?​

Yes. Start with menu = {}, add an empty inner dictionary with menu["tea"] = {}, then fill it with menu["tea"]["price"] = 2.5 and menu["tea"]["size"] = "cup". That gave {'tea': {'price': 2.5, 'size': 'cup'}}.

What does len() count in a nested dictionary?​

Only the keys of the dictionary you give it. With tea and cake on the menu, len(menu) gave 2, and len(menu["tea"]) gave 2 as well, for price and size.

How deep can dictionaries be nested?​

As deep as you need. {"shop": {"menu": {"tea": {"price": 2.5}}}} has dictionaries three levels down and printed as written. Every level adds one more pair of brackets when you use it, so two levels are usually enough.

What if I write the same key twice?​

The later one wins. A menu written with two "tea" entries kept only the second: {'tea': {'price': 9.9}}. The tutorial says: "If you store using a key that is already in use, the old value associated with that key is forgotten."

Can an inner dictionary hold a list?​

Yes. {"tea": {"price": 2.5, "sizes": ["cup", "pot"]}} printed with the list inside it. Values can be of any type.

Conclusion​

menu holds one inner dictionary per item, and menu["juice"] = {...} added a third one. menu["tea"] gives the inner dictionary for tea, and menu["tea"]["price"] = 2.8 changes one value inside it.

Additional Resources​