Skip to main content

Working with Nested Lists, Dicts, and Complex Data Structures

Intermediate25 min6 exercises85 XP
0/6 exercises

Think about a school. A school has classrooms. Each classroom has students. Each student has a name, an age, and a list of grades. That's data inside data inside data — nested data.

Nested data is everywhere in real programming. A social media app stores users who have posts, and each post has comments. A game stores levels that contain enemies, and each enemy has stats. An online store has categories containing products with reviews.

In this tutorial, you'll learn how to build, access, modify, and loop through nested lists and dictionaries. These skills are essential for working with real-world data, especially JSON from web APIs.

How Do Lists Inside Lists Work?

A list can contain other lists as its items. This creates a two-dimensional structure, like a grid or a table. Think of a spreadsheet — rows and columns.

A nested list representing a grid
Loading editor...

To access an item in a nested list, you use two sets of square brackets. The first bracket picks the row. The second bracket picks the column.

Accessing items with double indexing
Loading editor...

You can also modify items using the same double-bracket syntax. This is how you would place a move in a tic-tac-toe game.

Modifying items in a nested list
Loading editor...

How Do Nested Dictionaries Work?

A dictionary value can itself be another dictionary. This creates a tree-like structure where you drill down through keys to reach the data you need.

Nested dictionaries with chained key access
Loading editor...

Each set of square brackets digs one level deeper. school['Room 101'] gives you the inner dictionary. Then ['teacher'] gives you the value inside that inner dictionary.

You can also add new keys or update values at any level.

Modifying nested dictionaries
Loading editor...

Lists of Dictionaries: The Most Common Pattern

The most common nested structure in real-world programming is a list of dictionaries. Each dictionary represents one record — a student, a product, a user — and the list holds all the records together.

If you've ever seen data from a web API or a database, it almost always looks like a list of dictionaries.

A list of dictionaries — student records
Loading editor...

The pattern is always list[index][key]. The index picks which dictionary. The key picks which value inside that dictionary.

How to Access Deeply Nested Data Safely

Real-world data can be nested several levels deep. A company has departments, each department has teams, each team has members. Accessing deeply nested data means chaining multiple brackets.

Chaining brackets for deep access
Loading editor...

The danger with deep nesting is that any missing key causes a KeyError. If the data might be incomplete, use the .get() method, which returns None (or a default value) instead of crashing.

Safe nested access with .get()
Loading editor...

How to Loop Through Nested Data Structures

When you have nested data, you often need nested loops. The outer loop goes through the top-level items, and the inner loop goes through the items inside each one.

Looping through a dict of lists
Loading editor...
Looping through a 2D grid with enumerate
Loading editor...

For lists of dictionaries, a single loop is usually enough because each dictionary is one complete record.

Filtering a list of dicts with nested list checks
Loading editor...

How to Build Nested Data Structures from Scratch

You don't always receive nested data — sometimes you need to build it yourself. A common pattern is starting with an empty structure and adding items inside a loop.

Building a dict of lists by grouping
Loading editor...

Another common task is transforming flat data into nested structures. For example, turning a flat list of scores into a list of dictionaries.

Combining parallel lists into a list of dicts
Loading editor...

Practice Exercises

Access Nested List Items
Write Code

You have a 3x3 grid of numbers. Print the center value (row 1, column 1), then print the bottom-right value (row 2, column 2). Each on a separate line.

Loading editor...
Predict the Output: Nested Dict Access
Predict Output

Read the code carefully. What will it print? Type the exact output.

Loading editor...
Find the Highest Scorer
Write Code

Loop through the list of students and find the one with the highest grade. Print their name and grade in the format: Best: Name (Grade)

Loading editor...
Group Words by First Letter
Write Code

Group the words by their first letter into a dictionary. Then print each group on a separate line in the format: letter: [words]

Print the letters in sorted order.

Loading editor...
Fix the Bug: Nested List Trap
Fix the Bug

This code tries to create a 3x3 grid and set only the center cell to "X". But something is wrong — run it and see! Fix the bug so only the center cell is "X" and all other cells remain ".".

Print each row on a separate line.

Loading editor...
Calculate Class Averages
Write Code

You have a dictionary where each key is a class name and each value is a list of student dictionaries. Calculate the average grade for each class and print it in the format: ClassName: average

Round each average to 1 decimal place. Print classes in alphabetical order.

Loading editor...