Define a Nested Dictionary in Python - GeeksforGeeks

Define a Nested Dictionary in Python

Last Updated : 28 Sep, 2026

A nested dictionary is a dictionary in which one or more values are themselves dictionaries. It is useful for representing structured or hierarchical data, where related information can be grouped under different keys.

It can be defined in several ways depending on whether the structure is fixed or needs to be created dynamically. The commonly used methods are:

Using Curly Braces

Simplest way to create a nested dictionary is by placing one dictionary inside another. The outer dictionary contains keys whose values are inner dictionaries.

Example:

Python
student = {
    "name": "Alice",
    "details": {
        "age": 21,
        "course": "Python"
    }
}

print(student)

Output
{'name': 'Alice', 'details': {'age': 21, 'course': 'Python'}}

Explanation:

  • Outer dictionary contains the name and details keys.
  • Value associated with details is another dictionary.
  • This creates a two-level nested structure.

Using dict() Constructor

This constructor can also be used to create nested dictionaries. An inner dict() call can be used as the value of an outer key.

Example:

Python
student = dict(
    name="Alice",
    details=dict(
        age=21,
        course="Python"
    )
)

print(student)

Output
{'name': 'Alice', 'details': {'age': 21, 'course': 'Python'}}

Explanation:

  • Outer dict() creates the main dictionary.
  • Inner dict() creates the nested dictionary.
  • This approach can make small, fixed structures readable.

Using a For Loop

For loop can be used when the nested structure needs to be created from dynamic data. Instead of writing every inner dictionary manually, the required entries can be generated during iteration.

Example:

Python
students = {}

names = ["Alice", "Bob", "Charlie"]

for name in names:
    students[name] = {
        "course": "Python",
        "status": "Active"
    }

print(students)

Output
{'Alice': {'course': 'Python', 'status': 'Active'}, 'Bob': {'course': 'Python', 'status': 'Active'}, 'Charlie': {'course': 'Python', 'status': 'Active'}}

Explanation:

  • An empty outer dictionary is created first.
  • Each name becomes a key in the outer dictionary.
  • A new inner dictionary is assigned to every key.
  • This is useful when the number of entries is determined at runtime.

Using defaultdict

This class from the collections module can be used when nested dictionaries are built incrementally. It creates a default value for a missing key through its default_factory. With defaultdict(dict), a missing outer key automatically receives an empty dictionary when accessed with square brackets.

Example:

Python
from collections import defaultdict

students = defaultdict(dict)

students["Alice"]["course"] = "Python"
students["Alice"]["status"] = "Active"

print(dict(students))

Output
{'Alice': {'course': 'Python', 'status': 'Active'}}

Explanation:

  • defaultdict(dict) specifies dict as the default factory.
  • Accessing students["Alice"] creates an empty inner dictionary if Alice does not exist.
  • Values can then be added directly to that inner dictionary.
  • dict(students) converts the defaultdict into a regular dictionary for display.

Defining a Multi-Level Nested Dictionary

A dictionary can contain another dictionary, which can contain another dictionary. This allows multiple levels of hierarchical data to be represented.

Example:

Python
data = {
    "India": {
        "Haryana": {
            "city": "Gurgaon",
            "population": 1569000
        }
    }
}

print(data)

Output
{'India': {'Haryana': {'city': 'Gurgaon', 'population': 1569000}}}

Explanation:

  • India is the first level.
  • Haryana is stored inside the India dictionary.
  • city and population are stored inside the Haryana dictionary.
  • Additional levels can be created by nesting more dictionaries.
Comment