Skip to main content

Python Sets: Unique Collections, Operations, and Use Cases

Intermediate20 min6 exercises85 XP
0/6 exercises

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.

Creating sets with curly braces
Loading editor...

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.

Creating sets with the set() constructor
Loading editor...

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.

Union combines all unique items
Loading editor...

Intersection: Find What's in Common

An intersection gives you only the items that appear in both sets. Use the & operator or .intersection().

Intersection finds common items
Loading editor...

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.

Difference finds items unique to one set
Loading editor...

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().

Symmetric difference excludes shared items
Loading editor...

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.

Adding and removing set items
Loading editor...

You can also clear all items or pop a random item from the set.

Pop and clear methods
Loading editor...

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.

Fast membership testing with in
Loading editor...

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.

Frozensets are immutable sets
Loading editor...

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.

Use a List when...
# Order matters
playlist = ['song1', 'song2', 'song3']

# Duplicates are meaningful
die_rolls = [3, 5, 3, 2, 5, 6]

# You need index access
first = playlist[0]
Use a Set when...
# 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

Remove Duplicates from a List
Write Code

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().

Loading editor...
Predict the Set Output
Predict Output

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.

Loading editor...
Find Students in Both Classes
Write Code

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.

Loading editor...
Fix the Empty Set Bug
Fix the Bug

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.

Loading editor...
Find Missing Ingredients
Write Code

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.

Loading editor...
Deduplicate While Preserving Order
Write Code

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])).

Loading editor...