Working with Nested Lists, Dicts, and Complex Data Structures
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.
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.
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.
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.
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.
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.
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.
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.
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.
For lists of dictionaries, a single loop is usually enough because each dictionary is one complete record.
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.
Another common task is transforming flat data into nested structures. For example, turning a flat list of scores into a list of dictionaries.
Practice Exercises
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.
Read the code carefully. What will it print? Type the exact output.
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)
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.
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.
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.