Learn Python in 30 Days — Day 13: Nested Structures & Looping
Learn Python in 30 Days — Day 13: Nested Structures & Looping
Welcome to Day 13 of the Learn Python in 30 Days series!
Yesterday, we explored how to use dictionaries to store key/value data in a clean, flexible way.
Today, we take that one step further by combining structures together and looping through nested data.
If you’re aiming to become fluent in Python, understanding nested structures is essential. It’s how real-world data is represented from JSON APIs and databases to configuration files and game state tracking.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
Step 1 – What Are Nested Structures?
A nested structure is simply one data structure inside another.
Let’s start with a simple list:
Now imagine each number needs a label. A dictionary might work better:
But what if we have multiple of those dictionaries?
You can place them inside a list:
Now you have a list of dictionaries one of the most common Python patterns for real-world data.
Here’s an example program you can run in Python to demonstrate that concept in action, showing both the creation and the output from looping through your list of dictionaries:
Output:
Step 2 – Looping Through Nested Data
You’ve already learned how to loop through lists and dictionaries separately.
Now we’ll combine those skills.
Example: print each number’s label and ID.
Output:
Here’s what’s happening:
-
The
forloop iterates through each dictionary in the list. -
itemrefers to each dictionary in turn. -
You access each field with
item['id']anditem['label'].
Try it yourself, hopefully you'll see something like that shown below: -You can also download this example from my GitHub here and run it yourself.Step 3 – Nested Loops (Looping Inside Loops)
Let’s make it more interesting.
What if each number dictionary also contained a list?
Now you need a loop inside a loop to print everything:
Output:
Each time you loop over a dictionary, you can open a second loop to go deeper into its lists.
Try it yourself, hopefully you'll see something like that shown below: -You can also download this example from my GitHub here and run it yourself.Step 4 – Dictionaries Inside Dictionaries
Nesting isn’t limited to lists, you can nest dictionaries inside dictionaries too.
Example: imagine a mini contact book with contact details grouped neatly:
Loop through this nested dictionary like so:
Output:
Notice:
-
The outer loop iterates through each contact.
-
The inner loop goes through each contact’s details.
Try it yourself, hopefully you'll see something like that shown below: -You can also download this example from my GitHub here and run it yourself.Step 5 – Pretty Printing Complex Data
As your structures grow, printing them with print() can become messy.
Python includes a very handy module called pprint (pretty print).
Output (nicely formatted):
This is especially helpful when debugging nested dictionaries, JSON responses, or API data.
Try it yourself, hopefully you'll see something like that shown below: -You can also download this example from my GitHub here and run it yourself.Step 6 – Mini Project: Student Database
Let’s put it all together with a small nested structure project.
Goal: Create a student database that stores multiple students, each with their name, ID, and list of grades.
Output:
Try it yourself, hopefully you'll see something like that shown below: -You can also download this example from my GitHub here and run it yourself.💡 Tips
-
Use meaningful variable names when nesting deeply (e.g.,
student,course,item,entry). -
If your nesting gets more than 2–3 levels deep, consider breaking it into smaller parts or using classes.
-
The
jsonmodule can load and save nested structures easily (we’ll explore that later)
Next up — Day 14 – Week 2 Review
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
You can see the full series here Learn Python in 30 Days series!
Hope you have enjoyed this post, thanks Matty






Comments
Post a Comment