Python Sets: What They Are and How to Use Them
Table of Contents
Sets are like lists, but with two key differences: they are unordered and they guarantee uniqueness. Only one of each value can exist in a set. If you need to track unique items or remove duplicates, sets are the tool for the job.
All the content from our Boot.dev courses are available for free here on the blog. This one is the “Sets” chapter of Learn to Code in Python. If you want to try the far more immersive version of the course, do check it out!
How Do You Create a Set in Python?
Sets are declared using curly braces — the same braces used for dictionaries, but without key: value pairs:
fruits = {"apple", "banana", "grape"}
print(type(fruits)) # <class 'set'>
If you include duplicates, they’re silently dropped:
fruits = {"apple", "banana", "grape", "apple"}
print(fruits) # {'banana', 'grape', 'apple'}
There’s one gotcha: {} creates an empty dictionary, not an empty set. To create an empty set, use the set() constructor:
fruits = set()
How Do You Add and Remove Items From a Set?
The .add() method adds a single value to a set — think of it like .append() for lists:
fruits = {"apple", "banana", "grape"}
fruits.add("pear")
print(fruits) # {'pear', 'banana', 'grape', 'apple'}
Adding a value that already exists doesn’t raise an error — the set just stays the same.
To remove a value, use .remove():
fruits = {"apple", "banana", "grape"}
fruits.remove("apple")
print(fruits) # {'banana', 'grape'}
Be careful — .remove() raises a KeyError if the value doesn’t exist. You can use .discard() instead if you want to silently ignore missing values.
How Do You Loop Over a Set?
You can iterate over a set with a for loop just like a list:
fruits = {"apple", "banana", "grape"}
for fruit in fruits:
print(fruit)
The order of iteration is not guaranteed. If you need a consistent order, convert the set to a sorted list first with sorted().
How Do You Remove Duplicates With a Set?
One of the most common uses of sets is deduplication. You can convert a list to a set and back to strip out duplicates:
spells = ["fireball", "ice_blast", "fireball", "heal", "ice_blast"]
unique_spells = list(set(spells))
print(unique_spells) # ['heal', 'fireball', 'ice_blast']
The set() constructor removes duplicates, and list() converts it back. Keep in mind the order may change since sets are unordered.
You can also do this manually with a loop if you need to preserve order:
spells = ["fireball", "ice_blast", "fireball", "heal"]
seen = set()
unique = []
for spell in spells:
if spell not in seen:
seen.add(spell)
unique.append(spell)
print(unique) # ['fireball', 'ice_blast', 'heal']
The in check on a set is very fast — much faster than checking a list.
How Do You Check if a Value Is in a Set?
Use the in keyword — same syntax as lists and dictionaries:
vowels = {"a", "e", "i", "o", "u"}
print("e" in vowels) # True
print("x" in vowels) # False
Membership checks on sets are much faster than on lists. Under the hood, sets use a hash table, so lookups are roughly O(1) regardless of size. If you’re checking membership in a loop against a large collection, converting it to a set first can dramatically speed things up.
What Operations Can You Do With Sets?
Sets support mathematical operations. The most common is subtraction — removing all values in one set from another:
set1 = {"apple", "banana", "grape"}
set2 = {"apple", "banana"}
set3 = set1 - set2
print(set3) # {'grape'}
This is handy for finding what’s missing. For example, finding which IDs from one list are absent in another:
required = set([1, 2, 3, 4, 5])
present = set([2, 4])
missing = required - present
print(missing) # {1, 3, 5}
Python sets also support union and intersection:
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # {1, 2, 3, 4, 5} (union — all items from both)
print(a & b) # {3} (intersection — only shared items)
Union combines everything, intersection keeps only what’s in common. These operations are what make sets so powerful for comparing collections of data.
What Should You Learn After Python Sets?
You now know all four of Python’s core data structures: lists, dictionaries, tuples, and sets. From here, most learners dig deeper into functions and start tackling more complex problems that combine these structures.
If you want to keep going through the full Python curriculum with hands-on exercises, check out the Learn to Code in Python course on Boot.dev.
Frequently Asked Questions
What is the difference between a set and a list in Python?
A list is ordered and allows duplicate values. A set is unordered and guarantees uniqueness — each value can only appear once. Use a list when order matters or you need duplicates, and a set when you need to ensure uniqueness or do fast membership checks.
Why does {} create a dictionary instead of an empty set?
Python uses curly braces for both dictionaries and sets. Since dictionaries came first in the language, {} defaults to an empty dictionary. Use set() to create an empty set.
Are sets ordered in Python?
No. Sets are unordered, which means you cannot rely on the items being in any particular order when you iterate over them. If you need order, use a list.
Can you put a list inside a set in Python?
No. Set elements must be hashable (immutable). Lists are mutable, so they cannot be added to a set. You can use tuples instead since they are immutable.
Related Articles
Python Dictionaries: How to Create and Use Them
Mar 06, 2026 by lane
Dictionaries are one of Python’s most useful data structures. Instead of accessing values by a numeric index like you do with lists, you access them by a key — usually a string. If lists are like numbered shelves, dictionaries are like labeled drawers.
Python Lists: A Complete Beginner Guide
Mar 05, 2026 by lane
A natural way to organize and store data is in a list. Some languages call them “arrays”, but in Python we just call them lists. Think of all the apps you use and how many of the items in them are organized into lists — a social media feed is a list of posts, an online store is a list of products, the state of a chess game is a list of moves.
Python Loops: For, While, Break, and Continue
Mar 04, 2026 by lane
Loops let you run the same code over and over without rewriting it each time. Whether you need to count through numbers, process items in a list, or keep going until a condition changes, Python gives you for loops and while loops to handle it.
Python If Statements: If, Else, and Elif
Mar 03, 2026 by lane
Every useful program needs to make decisions. Python’s if statement is how you tell your code to do one thing or another depending on some condition — and once you understand if, elif, and else, you can handle just about any branching logic.