Python / Python Data Structures

Manipulating Nested Data Structures

This tutorial delves into the world of Python's nested data structures. By the end, you will understand how to create and manipulate complex data structures in your Python program…

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Introduces built-in data structures such as lists, tuples, dictionaries, and sets.

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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

Timestamp Converter

Convert timestamps to human-readable dates.

Use tool

Interest/EMI Calculator

Calculate interest and EMI for loans and investments.

Use tool

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

Lorem Ipsum Generator

Generate placeholder text for web design and mockups.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help