Python Day #13: Nesting Lists, Tuples, Dicts, and Sets in Python—Explained Simply
Learn how to nest Python data structures like lists, dictionaries, tuples, and sets with simple examples. Build real-world projects with clean, organized code.
At this point in Python you should have a basic to decent understanding of Data Structures. What are they? How do we use them?
In our last two articles, I broke down the four built-in data structures that come with Python. We first looked at Lists and Tuples, before we moved into Dictionaries and Sets. Each of these serve a distinct purpose and you got to see hands on how they work.
Now this was a big topic, you shouldn’t just jump past this as you need a moment to practice and reinforce these skills you’ve just learned in this new topic. At the bottom I have made you guys some mini data structure challenges you can use to test yourself!
In this lesson, we are going to discuss how we can use the different data structures to work together, this will cover nesting data structures. The term nesting you already know from our past lessons.
It’s here that we will begin to merge multiple different structures together to store more advanced data for our programs.
Every week you’ll be introduced to a new topic in Python, think of this as a mini starter course to get you going and allow you to have a structured roadmap that actually builds to create you a solid foundation in Python. Join us today!
In the grand scheme of things, we may use a single data structures like a list but this is rather basic. As we start to build real world applications we need to store a lot of data. You can think of JSON or an API for this, as this is quite common.
Over the last 8+ years I’ve been teaching full-time around the world which has given me the unique opportunity to work with over 1,500+ students. The last four years I’ve been teaching Python full-time. Breaking down complex topics, step by step.
I’ve spent this time curating a structured learning path that ensures my students have the greatest chance of success and don’t just learn but actually master Python as well as their logical thinking and problem solving.
In this article, you’re going to learn how to nest data structures. I will focus on creating a structure that uses all the different ones together. Remember that at the end of the article I will give you some quick challenges to see how well you understand these structures.
One point I should note is, spend time digesting these when I write them, after six weeks most of my posts are automatically put behind a paywall.
If you haven’t subscribed to my premium content yet, you should definitely check it out. You unlock exclusive access to all of these articles and all the code that comes with them, so you can follow along!
Plus, you’ll get access to so much more, like monthly Python projects, in-depth weekly articles, the '3 Randoms' series, and my complete archive!
I spend a lot of my week on these articles, so if you find it valuable, consider joining premium. It really helps me keep going and lets me know you’re getting something out of my work!
Thank you for allowing me to do work that I find meaningful. This is my full-time job so I hope you will support my work by joining as a premium reader today.
If you’re already a premium reader, thank you from the bottom of my heart! You can leave feedback and recommend topics and projects at the bottom of all my articles.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
P.S - Drop any questions in the comments or feel free to DM me directly as I’m always happy to help you guys out.
The Data Structures of Python
Before we jump right into the concept of nesting data structures, I am going to give you a quick glimpse at what we have covered in the last couple lessons. I’ll touch on the four data structures with a simple example to get the mind going.
Lists
A list is ordered, changeable, and they allow for duplicate values. Remember that list items are indexed, the first item has index [0]
, the second item has index [1],
etc.
When I say that lists are ordered, I just mean that the items have a defined order, and that order will not change.
The simple list above does the trick, and you have really three ways to make a list. You can use the square brackets []
, the python list()
function or the .split()
method. The split method only works on strings.
Tuples
A tuple is a collection of data that is ordered and unchangeable. This is so important, you can not change a tuple once you make it, it is a way to store data that you do not want to change.
Think of it like a RGB color. RGB stands for Red Green Blue. The scale starts at 0 and goes to 255. So for the color red you would have (255, 0, 0). Now even if you change one of those numbers the color would be different.
So in my above code block, I have a tuple. I can’t change any of this but I can still iterate through it with a loop and I can index an item just like we can in a List.
Three: Dictionaries
A dictionary is a collection of key-value pairs that are ordered, changeable and don’t allow duplicates for keys. The key must be an immutable type of data too.
An ordered dictionary just means that the items have a defined order, and that order will not change, and since the items do not have a defined order, we can’t refer to an item by using an index like we can a list or tuple.
These are really powerful as this is what working with JSON looks like so getting the hang of this is great. For keys since they are immutable you can only use: strings, integers, floats, booleans, and tuples as the key. The value can be any type of data.
Four: Sets
Finally a set would be a collection which is unordered, unchangeable, and unindexed. So we can not index this like a list or a tuple.
Another major distinction is a set does not allow for duplicates like a list and tuple. So if we add the same item multiple times, it will only ever be there once.
I did say above that a set is unchangeable, this is true but we are still able to add and delete items from sets. If you want to quickly remove all the duplicates of a list you can switch it to a set and back to a list again as a pro tip.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
Nesting Data Structures in Python
When we talk about nesting data structures in Python, we’re just talking about putting one type of data structure inside another. This is something you’ll run into a lot as you start building real programs, especially when the data you’re working with gets more detailed or layered.
For example, you might have a list that holds multiple dictionaries, or a dictionary where each value is actually a list. You can even have a list of lists. This kind of nesting is very common in real-world situations—especially when working with JSON data from APIs.
In those cases, you’ll almost always see a mix of lists, dictionaries, and sometimes even more layers.
Let’s break it down with a simple example. Imagine you’re keeping track of students in a class. You might create a list where each student is stored as a dictionary. That dictionary could have keys like "name"
, "age"
, and "scores"
, and the value for "scores"
might be a list of numbers.
Now you’ve got a list of dictionaries, and inside each dictionary is another list. That’s nesting in action. The reason we use this is because it helps us organize and structure complex data in a way that makes sense and is easy to work with.
You’re not limited to just one type of structure—you can combine them to fit the data you're handling. In the next section, we’ll walk through how to create these nested structures and how to pull the information you need from them.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
Ready to Take Your First Steps with Python?
Most people get stuck before they even start. But you don’t have to!
I’ve put together a Free Course: The Pure Basics of Python that covers the very basics of Python, made just for beginners like you.
👉 I’m giving you my exact system that’s been proven and tested by over 1,500 students over the last 4+ years!
Here’s what you’ll learn:
✅ Step-by-step help to set up VS Code and install Python.
✅ Learn how to use variables, built-in functions, and even nesting functions.
✅ A quick test to see how much you’ve picked up.
✅ My handcrafted Python Guide to keep you on track.
This free course is a perfect first step — you’ll get a feel for my teaching style and build confidence to keep going.
👉 Ready to get started?
P.S. — If you like the free course (and I think you will!), I’ll show you how to dive deeper with my full Python Masterclass.
🎁 - Get a free one-on-one coaching session with any Masterclass!
Go through the entire Masterclass, complete all material, and attend the Q&A's, if you still feel like your struggling I'll personally work with you one-on-one until you're confident!
Data Structure Challenges
Okay, now this is a fun activity I do with my students in the classroom. I often get carried away and write a long nested data structure on the board. I want you to analyze the code and write down the correct code to access the value I ask.
I will put the correct answers to these at the bottom of the article.
Challenge One
Challenge Two
Challenge Three
These got hard, but I hope you had fun with them. How do you think you did?
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
My Best Starter Resources
Here are the best resources I have to offer to get you started with Python no matter your background! Check these out as they’re bound to maximize your growth in the field.
Code with Josh: This is my YouTube channel where I post videos every week designed to help break things down and help you grow.
Zero to Knowing: My Python Masterclass platform designed from 8 years teaching experience and that has helped over 1,500+ students succeed with Python. (All my readers unlock a limited time, 25% discount, use code: learnpython25)
My Books: Maybe you’re looking to get a bit more advanced in Python. I’ve written 3 books to help with that, from Data Analytics, to SQL all the way to Machine Learning.
The Nerd Nook: This is where you are right now! I write here multiple times per week with the sole goal of breaking down this larger than life topic to help you excel and learn Python faster than you thought was possible.
This article will be a piece to a larger roadmap but will go behind a paywall after 6 weeks! To help support my work in teaching others consider joining as a premium reader to unlock all these articles, monthly Python projects and in-depth dives.
Answers to the Code Challenges
Here are the solutions to the coding challenges that I made you above.
Solution to Challenge One
Solution to Challenge Two
Solution to Challenge Three
Wrapping Up Lesson Thirteen
At this point, you should feel pretty comfortable with the core data structures in Python—lists, tuples, dictionaries, and sets. Now you’ve also seen how we can combine them using nesting.
This is where things start to click, because most data you’ll work with in the real world isn’t simple or flat. It’s layered. Whether you’re dealing with API responses, building a data model for a project, or just trying to keep things organized, nesting lets you build structures that match real-life situations.
As you move forward, try creating a few examples on your own. Maybe make a list of dictionaries, or a dictionary that holds lists, and just mess around with it. See what happens when you try to pull values out, add something new, or update the data.
The more you get your hands dirty, the easier this stuff gets. In the next lesson, we’ll keep building on this and look at more ways to work with and organize your data.
And hey—if this article helped you out, think about becoming a premium reader. You’ll get full access to every lesson, all the code examples, and a bunch of real-world Python projects that can really help you level up.
Let’s keep moving forward with Day #14 next week.
Hope you all have an amazing week nerds ~ Josh (Chief Nerd Officer 🤓)
👉 If you’ve been enjoying these lessons, consider subscribing to the premium version. You’ll get full access to all my past and future articles, all the code examples, extra Python projects, and more.