On this page

Nested Structures

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

What Nested Structures Are​

A nested structure is any data structure that contains another data structure inside it. Python allows you to combine lists, tuples, dictionaries, and sets in flexible ways, enabling you to model complex, real‑world data.

Examples of nested structures include:

Nested structures are essential for representing JSON data, API responses, configuration files, and multi‑dimensional datasets.

Why Nested Structures Matter​

Nested structures allow you to:

They are foundational for intermediate and advanced Python programming.


Lists Inside Lists​

A list can contain other lists, forming a multi‑dimensional structure.


matrix = [

    [1, 2, 3],

    [4, 5, 6],

    [7, 8, 9]

]

Accessing Nested List Elements​

Use multiple indices:


print(matrix[1][2])   # 6

This pattern is common in grid‑based data, tables, and numerical computations.


Dictionaries Inside Lists​

A list of dictionaries is one of the most common patterns in Python, especially when processing structured data.


users = [

    {"name": "Alice", "age": 30},

    {"name": "Bob", "age": 25}

]

Accessing Values​


print(users[0]["name"])   # Alice

This structure mirrors JSON arrays returned by APIs.


Lists Inside Dictionaries​

Dictionaries often store lists as values.


student = {

    "name": "Sara",

    "scores": [85, 90, 92]

}

Accessing Nested Values​


print(student["scores"][1])   # 90

This pattern is ideal for representing entities with multiple attributes.


Dictionaries Inside Dictionaries​

Nested dictionaries allow you to build multi‑level structures.


config = {

    "database": {

        "host": "localhost",

        "port": 5432

    }

}

Accessing Nested Keys​


print(config["database"]["host"])

This is the basis of configuration files and hierarchical data.


Tuples Inside Other Structures​

Tuples are often used to store fixed‑size records inside lists or dictionaries.


points = [

    (10, 20),

    (30, 40)

]

Tuples ensure that coordinate pairs remain unchanged.


Sets Inside Other Structures​

Sets can appear inside lists or dictionaries, but cannot contain mutable elements such as lists or dictionaries.

Valid:


groups = {

    "admins": {"alice", "bob"},

    "guests": {"tom"}

}

Invalid:


# {"a", ["b", "c"]}   # lists cannot be inside sets

Sets are ideal for fast membership testing within nested structures.


Looping Through Nested Structures​

Nested loops allow you to process multi‑level data.

Example: Looping Through a List of Dictionaries​


for user in users:

    print(user["name"], user["age"])

Example: Looping Through a Matrix​


for row in matrix:

    for value in row:

        print(value)

Nested loops are essential for traversing hierarchical data.


Modifying Nested Structures​

Because lists and dictionaries are mutable, you can update nested values easily.


users[1]["age"] = 26

matrix[0][1] = 99

Always ensure you understand the structure before modifying it.


When to Use Nested Structures​

Use nested structures when:

Nested structures provide clarity, flexibility, and expressive power.


Summary​

Nested structures combine Python’s core data types—lists, tuples, dictionaries, and sets—to represent complex, layered information. They are essential for real‑world programming, data processing, and interacting with modern APIs. Mastering nested structures prepares you for advanced topics such as data modeling, serialization, and working with external data formats.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Sets

Next

Iterating Over Complex Data