CodingNic

Data Structures

Nested Data Structures

Data Structures 26 min read

Nested Data Structures

Nested Data Structures

Sometimes one data structure is not enough.

Real programs often store complex data such as:

  • A student with many subjects
  • A product with many details
  • A class with many students
  • A store with many products

To do this, Python allows one data structure inside another.

This is called nesting.

What Does Nested Mean?

Nested means placed inside something else.

Examples:

  • A list inside a list
  • A dictionary inside a dictionary
  • A list inside a dictionary
  • A dictionary inside a list

Why Nested Data Matters

Nested data helps you organize real information in a clean way.

Instead of many random variables, related data stays together.

List Inside a List

python
numbers = [
    [1, 2, 3],
    [4, 5, 6]
]

print(numbers)

Output

text
[[1, 2, 3], [4, 5, 6]]

Access Nested List Items

Use two indexes.

python
print(numbers[0][1])
print(numbers[1][2])

Output

text
2
6

Explanation:

  • numbers[0] = first inner list
  • numbers[0][1] = second item inside first list

Dictionary Inside a Dictionary

python
user = {
    "name": "Tom",
    "address": {
        "city": "New York",
        "country": "USA"
    }
}

print(user["address"]["city"])

Output

text
New York

List Inside a Dictionary

python
student = {
    "name": "Sara",
    "scores": [80, 90, 75]
}

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

Output

text
[80, 90, 75]
90

Dictionary Inside a List

python
students = [
    {"name": "Tom", "score": 85},
    {"name": "Sara", "score": 92}
]

print(students[0]["name"])
print(students[1]["score"])

Output

text
Tom
92

Updating Nested Data

You can change values inside nested data.

python
student = {
    "name": "Sara",
    "scores": [80, 90, 75]
}

student["scores"][0] = 100

print(student)

Output

text
{'name': 'Sara', 'scores': [100, 90, 75]}

Loop Through Nested Data

python
students = [
    {"name": "Tom", "score": 85},
    {"name": "Sara", "score": 92}
]

for student in students:
    print(student["name"], student["score"])

Output

text
Tom 85
Sara 92

Real Example: Store Products

python
products = [
    {"name": "Laptop", "price": 900},
    {"name": "Phone", "price": 500}
]

for product in products:
    print(product["name"], product["price"])

Output

text
Laptop 900
Phone 500

Code Along

python
classroom = {
    "teacher": "Maya",
    "students": ["Tom", "Sara", "John"]
}

print(classroom["teacher"])
print(classroom["students"][0])

Output

text
Maya
Tom

Mini Challenge

Build a movie collection.

Steps:

  • Create a list of dictionaries with 2 movies
  • Each movie should have:
    title, year
  • Print the first movie title
  • Print the second movie year
  • Loop through all movies and print title and year

Expected output:

text
Inception
2020
Inception 2010
Avatar 2020

Real World Use Case

Programs use nested data for APIs, shopping carts, user profiles, dashboards, games, and databases.

Quiz

  1. What does nested mean?
  2. How do you access a list inside a dictionary?
  3. Can dictionaries be stored in a list?
  4. Why is nested data useful?

Assignment

Create a dictionary for a school. Add a list of students inside it. Print the school name and all student names.

Summary

You learned how Python stores data structures inside other data structures and how nested data is used in real programs.