Manipulating Nested Data Structures

Tutorial 4 of 5

Tutorial: Manipulating Nested Data Structures in Python

1. Introduction

  • Goal: This tutorial aims to help you understand how to create and manipulate nested data structures in Python.
  • Learning Outcomes: Upon completing this tutorial, you will be able to create, access, and manipulate complex nested data structures.
  • Prerequisites: Basic knowledge of Python, including data types (like lists, tuples, dictionaries), control flow (if-else, for loops), and basic functions.

2. Step-by-Step Guide

In Python, we usually deal with data structures like lists, tuples, and dictionaries. Nested data structures are when these data structures contain other data structures within them.

For example, consider a list of dictionaries. Each dictionary represents a person, and the list represents a group of people.

people = [
    {"name": "Alice", "age": 25},
    {"name": "Bob", "age": 30}
]

Here, people is a nested data structure.

Accessing Nested Data Structures

To access the data in nested structures, you chain the access methods. For a list of dictionaries like above, to access Bob's age, you'd do:

bob_age = people[1]["age"] # As Bob is the second item in the list (index 1) and age is a key in the dictionary.

Manipulating Nested Data Structures

You can also modify nested data structures. To change Bob's age, you'd do:

people[1]["age"] = 35

3. Code Examples

Example 1: Accessing data in a nested dictionary

# A dictionary representing a student
student = {
    "name": "Alice",
    "grades": {"math": 90, "science": 85, "english": 88}
}

# Accessing Alice's math grade
math_grade = student["grades"]["math"]
print(math_grade)  # Outputs: 90

Example 2: Modifying data in a nested list

# A list of lists, representing a matrix
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

# Changing the value 6 to 60
matrix[1][2] = 60
print(matrix)  # Outputs: [[1, 2, 3], [4, 5, 60], [7, 8, 9]]

4. Summary

In this tutorial, we've covered how to create, access, and manipulate nested data structures in Python. You should now feel comfortable working with complex data structures.

Next Steps

Try to use nested data structures in your programs. They are especially useful when working with large, complex data.

Additional Resources

  • Python Documentation: Data Structures - https://docs.python.org/3/tutorial/datastructures.html
  • Python For Beginners: Data Structures - https://www.pythonforbeginners.com/basics/python-data-structures

5. Practice Exercises

Exercise 1:

Create a dictionary representing a movie. The dictionary should contain title, year, and a list of actor names. Print the title and the first actor in the list.

Exercise 2:

Given a list of dictionaries, each representing a person with a name and age, write a function that prints the name of the oldest person.

Exercise 3:

Given a dictionary where each key is a student name and the value is a list of test scores, write a function that prints each student's highest score.

Solutions

  • Will be shared in the next tutorial.
  • For further practice, try to solve more complex problems involving nested data structures.