Python Day #12: Dictionaries vs Sets: What They Are and When to Use Them
Learn how to use Python dictionaries and sets. Discover key differences, real-world examples, and why these data structures matter in modern Python.
Bam, you’ve wrapped your head around lists and tuples, those are the two data structures we looked at last week, a great way to ease you into these concepts.
Since you now have an understanding on these two data structures and I’m hoping you had some time to practice with them, we can now move into the final two built-in data structures Python has to offer.
At the stage you’ve been introduced and worked with lists and tuples. The next two data structures we will look at today will be Dictionaries and Sets. These are going to bring you a lot of new tools and ways to manipulate and store data.
Just like lists and tuples, dictionaries and sets serve a distinct purpose so we need to look at when and why we use them as you begin to learn how to use them as they each have their own use cases with custom functions.
Welcome back to the Python Roadmap to Success, where I am giving all my readers a fair chance at learning Python the right way, following a proven roadmap that I’ve structured and taught for over the last four years.
In our last article, we covered the eleventh phase in the Roadmap to Success, it covered your introduction to data structures with lists and tuples. This lesson will build on your new core understanding of data structures in Python.
In this lesson, you are going to learn the next two of the four built-in data structures that Python has to offer. I have ensured that these go in order as this is the same path I take my students through one on one. We will look at Dictionaries and Sets to get started.
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!
More or less, the role of a data structures is to store more than one value, and often it may even be multiple values of different types of data. This is why we work with data structures. But dictionaries and sets bring some real value to us as developers.
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, we’re going to look at two new data structures. These being dictionaries and sets. Dictionaries are common and slightly more sophisticated as they allow us to really nest and work with our data. Sets are similar to lists but with important key differences and powers we will look at.
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.
What is a Dictionary?
We now know that Data Structures are ways that we organize and structure data in programming. In Python we have the four built-in data structures that are quite common to see and use. They each serve a different purpose and can do different things.
Since last week we looked at lists and tuples, we can move into more advanced structures. As they can do so much but we also use these to get started with more advanced applications and programs that include JSON and API’s.
Let me start with a Dictionary. This is basically like a library in a way. Every item has a key to locate the item and the key has a value. We can add items, store multiple values of different data types, edit and even delete items.
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.
So when I say that dictionaries are ordered, it 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.
Read the last few sentences again, these are all important features of a dictionary. There are two ways we define a new dictionary. The first is by simply using the curly brackets {}
, any time you see these brackets I want you to think about dictionaries.
The other way is to use the Python dict()
function. This is good for new empty dictionaries and also allows us to nest functions to work with dictionaries. There are some functions you can look into such as items
, get
, keys
, and values
. Think about how these can be used.
👉 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!
How Do We Use a Dictionary?
Really to define a dictionary is a little different, but hopefully straightforward. We just make a variable, which will be the name of the dictionary, then the value is set to a pair of curly brackets {}
or the dict()
function we spoke about.
If you have data to begin with we use the curly brackets, otherwise you can use either of the options to make the dictionary. Below is a quick way to view a dictionary and get specific values from the dictionary.
The above is a perfect starter on how to get started with the pure basics of dictionaries, now I do actually cover all these data structures in a lot more detail through interactive lessons and code challenges to help make sure you understand each step in my Python Masterclass.
Since you have wrapped your head around the basics we can now look at some functionalities we use to work with dictionaries. You will use these often and should get comfortable with them.
I am only going to touch on a few but these ones will be the most crucial for your development. With time you will naturally pick up the others and as you should be self studying you will see them.
In order to add a key-value pair to a dictionary we typically use standard Python. I want you to remember this: dictionary[key] = value,
dictionary key unlocks a value.
Say it again, back to yourself. Say it 10 times, never forget this as this will make everything click and become easier. A dictionary key will unlock/return a value.
👉 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!
Anytime we want to access the value in a dictionary we just need the key. Anytime you delete a key, the value associated with that key is automatically deleted. So in order to delete an item from a dictionary we often use the Python del
keyword.
I will let you take a moment to read through the code I have above, but this is how all this looks if we write out a script to run. Remember to start trying this and practicing on your own because that is going to make all the difference!
👉 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!
What is a Set?
Alright, our last data structure is a set. These are similar to lists or tuples but have some hidden features or should I say powers.
Remember what a list is? Lists are ordered, changeable, and they allow duplicate values. Now 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.
I often find myself using these when firstly I do not want duplicates, this makes it easy. The other times when I want to compare the sets together. I can search them for similarities, differences and even merge sets into one faster.
In order to create a set we just use a pair of curly brackets {}
, just like a dictionary. You can also of course use the set()
function. Then just like a list you can add multiple elements to it and work with it.
Let’s look at a set example and showcase some of the special set methods to actually work with them better. You can see I am using three special methods: intersection
, union
, and difference
.
I will begin with the merge function. This is union, this allows you to basically add two sets together, this will create a new set from that. Remember that any duplicates will automatically be removed from this new set.
Next up we have intersection. This method is great as it allows you to create a new set from two sets. This new set will return what the two sets have in common, so you can see how this might become handy over time.
Finally we have difference. This one is kind of like subtraction. We can determine what set A has that set B does not have, etc. This will also return a set that contains items that exist only in the first set, and not in both sets.
For each of these you can also use an operator instead of the actual method. You can replace the union
method with the |
operator, you can replace the intersection
method with the &
operator, and the difference
method can be replaced with the -
operator.
Guys, I have covered the next two important data structures to get your heads wrapped around these topics. I want you to spend some time this week trying to write out your own programs to included these two data structures.
👉 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.
Wrapping Up Lesson Twelve
So now you’ve gone through all four of Python’s built-in data structures — lists, tuples, dictionaries, and sets. Each one has its own job, and by this point, you should feel pretty comfortable using them to organize and work with your data.
Dictionaries are great when you need to pair things up, like names and phone numbers, and want to look stuff up fast. Sets are super useful when you don’t want any duplicates and need to compare or combine different groups of data. These are tools you’ll use a lot, especially as your code starts to get more advanced.
Now’s the time to really put this into practice. Try writing some small programs that use dictionaries and sets. Mix them in with the lists and tuples you learned last week. The more you use them, the more natural it’ll start to feel.
And remember, we’re just getting started here. Each week builds on the last, and I’ve laid this out in a way that gives you a real shot at learning Python the right way. If these lessons are helping you out, take a look at the premium content. That’s where you’ll find deeper projects, hands-on challenges, and even one-on-one help if you need it.
Learning Python isn’t just about writing code — it’s about building a real skill you can actually use.
Alright, I’ll see you in the next one. Keep practicing, keep asking questions, and keep showing up.Let’s keep moving forward with Day #13 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.