Python Sets: Unique Collections, Operations, and Use Cases
Imagine you're a teacher taking attendance. A student accidentally signs the attendance sheet twice. Does that mean they're in class twice? Of course not. You only care about who showed up, not how many times they wrote their name.
A Python set works the same way. It's a collection that automatically removes duplicates. Every item in a set is unique. Sets also don't care about order — they just care about membership.
In this tutorial, you'll learn how to create sets, perform powerful operations like union and intersection, modify sets with methods, and discover when sets are the perfect tool for the job.
How to Create a Set in Python
You create a set by placing items inside curly braces {}, separated by commas. If you add duplicate values, Python quietly ignores them.
Notice that the output order might look different from what you typed. Sets are unordered, so Python can store items in any arrangement internally. Don't rely on sets to keep a specific order.
You can also create a set from a list using the set() constructor. This is a popular way to remove duplicates from a list.
Set Operations: Union, Intersection, and Difference
The real power of sets comes from set operations. These let you combine, compare, and find differences between sets. Think of them like Venn diagrams from math class.
Union: Combine Everything Together
A union gives you all items from both sets, without duplicates. You can use the | operator or the .union() method.
Intersection: Find What's in Common
An intersection gives you only the items that appear in both sets. Use the & operator or .intersection().
Difference: What's Unique to One Set
A difference gives you items that are in the first set but not in the second. Use the - operator or .difference(). The order matters here.
Symmetric Difference: Items in One but Not Both
A symmetric difference gives you items that are in either set, but not both. Think of it as the opposite of intersection. Use the ^ operator or .symmetric_difference().
How to Add and Remove Items from a Set
Sets are mutable, meaning you can add and remove items after creation. Here are the key methods you'll use.
You can also clear all items or pop a random item from the set.
Why Sets Are Lightning-Fast for Membership Testing
One of the best reasons to use a set is checking if something exists. The in keyword works with both lists and sets, but sets are dramatically faster.
What Are Frozensets?
A frozenset is an immutable version of a set. Once created, you cannot add or remove items. This makes frozensets hashable, meaning they can be used as dictionary keys or stored inside other sets.
When Should You Use a Set Instead of a List?
Sets and lists solve different problems. Here's a simple rule: if you care about order or duplicates, use a list. If you care about uniqueness or fast lookups, use a set.
# Order matters
playlist = ['song1', 'song2', 'song3']
# Duplicates are meaningful
die_rolls = [3, 5, 3, 2, 5, 6]
# You need index access
first = playlist[0]# Only unique items matter
tags = {'python', 'coding', 'tutorial'}
# Fast membership checks
if 'python' in tags:
print('Found!')
# Removing duplicates
unique = set([1, 2, 2, 3, 3])Practice Exercises
You have a list of email addresses where some appear more than once. Create a set called unique_emails from the emails list and print it sorted alphabetically using sorted().
What does this code print? Think carefully about how sets handle duplicates and what the len() function returns.
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
unique = set(data)
print(len(unique))Write a print() statement with the exact output.
You have two sets: math and english. Find and print the students who are enrolled in both classes. Print the result as a sorted list.
This code tries to create an empty set, add items to it, and print the result. But there's a bug — the programmer used {} to create an empty set. Fix the bug so the code works correctly.
You have a recipe set with all required ingredients and a pantry set with what you already have. Find the ingredients you still need to buy and print them as a sorted list.
Write a function called dedupe(items) that removes duplicates from a list while preserving the original order. Use a set to track which items you've already seen. Return the deduplicated list.
Test it by calling print(dedupe([3, 1, 4, 1, 5, 9, 2, 6, 5, 3])).