Course Progress30%
🍎 Python Basics Topic 30 / 100
⏳ 5 min read

Sets

Unordered collections of unique values — the fastest way to remove duplicates and check membership in Python.

"A set has one rule: every item must be unique. That one rule solves a surprising number of real problems."

— ShurAI

What is a Set?

A set is like a bag where you can only put each item in once. Add the same thing twice — it still only appears once. Sets are unordered (no index positions) and contain only unique values:

python
# Create a set — curly braces, no colons (different from dict!)
fruits = {"apple", "mango", "banana", "apple"}
print(fruits)     # {'mango', 'apple', 'banana'} — duplicate removed!
print(type(fruits)) # <class 'set'>

# Fastest way to remove duplicates from a list
names = ["Riya", "Arjun", "Riya", "Sneha", "Arjun"]
unique_names = list(set(names))
print(unique_names)  # ['Sneha', 'Arjun', 'Riya'] (order may vary)
Empty set — use set(), not {}

{} creates an empty dictionary, not a set. To create an empty set, write set().

Adding and Removing

python
tags = {"python", "coding"}

tags.add("beginners")     # add one item
tags.add("python")        # duplicate — silently ignored
print(tags)               # {'python', 'coding', 'beginners'}

tags.remove("coding")      # remove (raises error if missing)
tags.discard("missing")   # safe remove — no error if missing
print(tags)               # {'python', 'beginners'}

Membership Testing — Very Fast

Checking x in set is much faster than checking x in list for large collections. Sets are optimised for this:

python
banned_users = {"spammer99", "troll42", "bot123"}

username = input("Enter username: ")
if username in banned_users:
    print("Access denied.")
else:
    print("Welcome!")

Set Operations — Maths Made Easy

Sets support union, intersection and difference — the same concepts from maths, written cleanly in Python:

python
python_students = {"Riya", "Arjun", "Sneha"}
java_students   = {"Arjun", "Vikram", "Kavya"}

# Union | — students in EITHER class
print(python_students | java_students)
# {'Riya', 'Arjun', 'Sneha', 'Vikram', 'Kavya'}

# Intersection & — students in BOTH classes
print(python_students & java_students)
# {'Arjun'}

# Difference - — only in Python, not Java
print(python_students - java_students)
# {'Riya', 'Sneha'}
A | B
Union
Everything in A or B
A & B
Intersection
Only in both A and B
A - B
Difference
In A but not in B

Real Example — Remove Duplicate Votes

python
# Raw votes — some people voted twice!
raw_votes = ["Riya", "Arjun", "Riya", "Sneha",
             "Arjun", "Kavya", "Riya"]

valid_votes = set(raw_votes)

print(f"Raw votes  : {len(raw_votes)}")   # 7
print(f"Valid votes: {len(valid_votes)}")  # 4
print(f"Voters     : {valid_votes}")
output
Raw votes  : 7
Valid votes: 4
Voters     : {'Sneha', 'Kavya', 'Riya', 'Arjun'}

Quick Comparison: list vs set vs dict

Type Ordered Unique Access by Best for
list Yes No index [0] Ordered sequences
dict Yes* Keys key ["name"] Named data, lookups
set No Yes not indexable Uniqueness, fast lookup

"When you need to know 'is this in the collection?' and you don't care about order, use a set. It's faster and says exactly what you mean."

— ShurAI

🧠 Quiz — Q1

What happens when you add a duplicate item to a set?

🧠 Quiz — Q2

How do you create an empty set?

🧠 Quiz — Q3

What does A & B return for two sets?

🧠 Quiz — Q4

What is the quickest way to remove duplicates from a list in Python?