Handling Nested Data Structures in Python

Techniques for Working with Complex Data Hierarchies

Learn how to manage and manipulate nested data structures in Python, including lists of dictionaries, dictionaries of lists, and deeply nested objects. This guide provides practical techniques for accessing, iterating, and transforming complex, nested data.

Programming
Author
Affiliation
Published

February 9, 2024

Modified

February 9, 2025

Keywords

nested data structures Python, manipulating nested data, Python nested lists, Python nested dictionaries

Introduction

In many real-world applications, data is not always flat. Instead, it often comes in nested formats—such as lists of dictionaries, dictionaries containing lists, or even more deeply nested structures. Handling this kind of complex data can be challenging, but with the right techniques, you can access, iterate, and transform nested data efficiently. This tutorial provides practical examples to help you master these skills in Python.



Accessing Nested Elements

When dealing with nested data, the first step is knowing how to access elements at various levels. Consider a simple example of a nested dictionary representing a student’s record:

#| label: nested-access
student = {
    "name": "Alice",
    "grades": {
        "math": 90,
        "science": 85,
        "history": 88
    },
    "activities": ["basketball", "chess", "volunteering"]
}

# Accessing nested values:
math_grade = student["grades"]["math"]
first_activity = student["activities"][0]

print("Math Grade:", math_grade)
print("First Activity:", first_activity)

In this example, we access the math grade from a nested dictionary and the first activity from a nested list.

Output:

Math Grade: 90
First Activity: basketball

Iterating Through Nested Data

Often, you’ll need to iterate over nested structures to extract or transform information. For example, consider a list of student records, where each record is a dictionary containing grades:

#| label: iterate-nested
students = [
    {"name": "Alice", "grades": {"math": 90, "science": 85}},
    {"name": "Bob", "grades": {"math": 75, "science": 80}},
    {"name": "Charlie", "grades": {"math": 88, "science": 92}}
]

# Extract math grades for all students
math_grades = [student["grades"]["math"] for student in students]
print("Math Grades:", math_grades)

This list comprehension iterates over each student record and extracts the math grade, demonstrating a simple way to traverse nested data.

Output:

Math Grades: [90, 75, 88]

Transforming Nested Data

Sometimes, you may need to restructure nested data to make it easier to work with. For instance, converting a dictionary of lists into a list of dictionaries can be very useful.

#| label: transform-nested
data = {
    "name": ["Alice", "Bob", "Charlie"],
    "age": [25, 30, 35],
    "city": ["New York", "Los Angeles", "Chicago"]
}

# Convert to a list of dictionaries
flattened_data = [dict(zip(data, t)) for t in zip(*data.values())]
print("Flattened Data:", flattened_data)

This transformation uses the zip function and dictionary comprehensions to reformat the nested data structure into a more usable format.

Output:

Flattened Data: [
  {'name': 'Alice', 'age': 25, 'city': 'New York'}, 
  {'name': 'Bob', 'age': 30, 'city': 'Los Angeles'}, 
  {'name': 'Charlie', 'age': 35, 'city': 'Chicago'}
  ]

Combining Multiple Operations

In real-world scenarios, you may need to perform several operations on nested data. For example, merging two dictionaries and then filtering based on a condition:

#| label: combined-operations
data1 = {"Alice": 25, "Bob": 30}
data2 = {"Charlie": 35, "Bob": 32}

# Merge dictionaries (data2 values overwrite data1 for duplicate keys)
merged_data = {**data1, **data2}

# Filter out entries where age is below 30
filtered_names = [name for name, age in merged_data.items() if age >= 30]
print("Filtered Names:", filtered_names)

This example demonstrates merging two dictionaries and then filtering out keys based on the associated values.

Output:

Filtered Names: ['Bob', 'Charlie']

Conclusion

Handling nested data structures is a critical skill in Python, especially when working with complex real-world data. By learning how to access, iterate, and transform nested data, you can write more efficient and readable code. Experiment with these examples and adapt them to your specific needs to master the art of managing complex data hierarchies.

Further Reading

Happy coding, and enjoy exploring complex, nested data structures in Python!

Back to top

Reuse

Citation

BibTeX citation:
@online{kassambara2024,
  author = {Kassambara, Alboukadel},
  title = {Handling {Nested} {Data} {Structures} in {Python}},
  date = {2024-02-09},
  url = {https://www.datanovia.com/learn/programming/python/additional-tutorials/nested-data-structures.html},
  langid = {en}
}
For attribution, please cite this work as:
Kassambara, Alboukadel. 2024. “Handling Nested Data Structures in Python.” February 9, 2024. https://www.datanovia.com/learn/programming/python/additional-tutorials/nested-data-structures.html.