Sets (unique items)
๐ What are Sets? A set is an unordered collection that stores only unique items - duplicates are automatically removed. If you add the same item twice, the set only keeps one copy. Sets are extremely fast for checking membership (if x in my_set) which is far quicker than searching a list.

Appy Saysโฆ
Need to find every unique visitor to your app? Remove duplicate entries from a list? Check if two groups share any members? Python's set does all three instantly โ and it's way faster than a loop.
What is a Set?
A set is an unordered collection of unique items. Duplicate values are automatically ignored. Sets support fast membership testing (in) and powerful mathematical operations like union, intersection, and difference.
- โขCreate:
my_set = {1, 2, 3}orset([1, 2, 2, 3])โ{1, 2, 3} - โขEmpty set:
set()โ NOT{}(that creates an empty dict!) - โข
.add(item)โ add one item;.remove(item)โ remove (error if missing);.discard(item)โ remove silently - โข
a | bโ union (all items from both) - โข
a & bโ intersection (items in both) - โข
a - bโ difference (items in a but not b) - โข
a ^ bโ symmetric difference (items in one but not both)
Think of it like a Minecraft inventory with unique items only
Imagine an inventory where each item type can only appear once โ no stacks. If you try to add a second diamond sword, nothing changes. That's a set. And you can instantly compare two inventories to find what's common or what's exclusive to each.
How It Works
- โข1. Sets use a hash table internally โ membership check is O(1), regardless of set size
- โข2. When you add a duplicate, Python silently ignores it โ no error
- โข3. Order is not guaranteed โ don't rely on iteration order
- โข4. Only hashable (immutable) items can be in a set: ints, strings, tuples โ not lists or dicts
- โข5. Convert a list to a set to remove duplicates:
unique = list(set(my_list)) - โข6.
item in my_setis much faster thanitem in my_listfor large collections
Real-World Examples
- โขCount unique daily users:
len(set(user_ids_today)) - โขFind common friends:
friends_of_alice & friends_of_bob - โขFind songs in one playlist but not another:
playlist_a - playlist_b - โขRemove duplicate tags from a blog post:
tags = list(set(tags)) - โขCheck if a username has been taken:
if username in registered_users_set:
Key Facts
- โขSet membership (
in) is O(1) average โ checking a million-item set is as fast as checking a 10-item set - โขLists have O(n) membership โ a set is ~100,000ร faster for large collections
- โขPython also has
frozensetโ an immutable set that can be used as a dictionary key - โขSet comprehensions work just like list comprehensions:
{x*2 for x in range(5)}
Watch Out!
{} creates an empty dictionary, NOT an empty set. For an empty set you must write set(). This trips up almost every Python beginner at least once.
Remember
Use a set whenever you need uniqueness or fast membership checking. It's the right tool for 'have I seen this before?' logic.
What You Learned
- โขSets store unique items; duplicates are silently ignored
- โขOperations:
|union,&intersection,-difference โ all very fast - โขUnlocks: deduplication, fast lookups, comparing groups, unique visitor counting
Key Facts
- โSet membership (
in) is O(1) average โ checking a million-item set is as fast as checking a 10-item set - โLists have O(n) membership โ a set is ~100,000ร faster for large collections
- โPython also has
frozensetโ an immutable set that can be used as a dictionary key - โSet comprehensions work just like list comprehensions:
{x*2 for x in range(5)}
Real-World Examples
Remember
Use a set whenever you need uniqueness or fast membership checking. It's the right tool for 'have I seen this before?' logic.
Quick Quiz
Sets contain?