Lesson 15 of 19 · 3 min
Sets — Unique Values, No Duplicates
WHY Sets?
A set is a collection where each item can appear at most once. Perfect for: (1) removing duplicates from a list, (2) finding common elements between two groups, (3) finding what is in one group but not another. Sets use a hash table internally, making membership checks O(1) — nearly instant even for millions of items.
Creating Sets
fruits = {"apple", "banana", "mango", "apple", "banana"} # duplicates in literal
print(fruits) # {'mango', 'apple', 'banana'} — duplicates gone, order varies!
empty_set = set() # MUST use set() for empty set!
print(type(empty_set)) # <class 'set'>
# {} creates an empty DICTIONARY, not a set
# Convert list → set to remove duplicates instantly
ids = [101, 102, 103, 102, 101, 104, 103]
unique = set(ids)
print(unique) # {101, 102, 103, 104} — only unique values kept
Output:
{'mango', 'apple', 'banana'}
<class 'set'>
{101, 102, 103, 104}Adding and Removing
skills = {"Python", "SQL", "Excel"}
skills.add("Tableau") # add one item — no error if already present
print(skills) # {'Python', 'SQL', 'Excel', 'Tableau'}
skills.remove("Excel") # remove — raises KeyError if NOT found!
print(skills) # {'Python', 'SQL', 'Tableau'}
skills.discard("Excel") # SAFE remove — no error if not found
skills.discard("PowerBI") # does nothing — PowerBI not in set, no crash
print(skills) # {'Python', 'SQL', 'Tableau'}
print("Python" in skills) # True — O(1) lookup, very fast!
print("Excel" in skills) # False
Output:
{'Python', 'SQL', 'Excel', 'Tableau'}
{'Python', 'SQL', 'Tableau'}
{'Python', 'SQL', 'Tableau'}
True
FalseSet Mathematics — Finding Relationships Between Groups
monday = {"Priya", "Ahmed", "Ravi", "Sara", "Meera"}
tuesday = {"Ahmed", "Sara", "Arjun", "Riya", "Ravi"}
# Union — all students who attended EITHER day (no duplicates)
all_s = monday | tuesday
print(f"All: {all_s}")
# Intersection — students who attended BOTH days
both = monday & tuesday
print(f"Both days: {both}")
# Difference — attended Monday but NOT Tuesday
only_mon = monday - tuesday
print(f"Monday only: {only_mon}")
# Symmetric difference — attended exactly ONE day (not both)
one_day = monday ^ tuesday
print(f"One day only: {one_day}")
# Subset / Superset checks
small = {"Ahmed", "Sara"}
print(small.issubset(monday)) # True — both members are in monday
print(monday.issuperset(small)) # True — monday contains all of small
Output:
All: {'Priya', 'Ahmed', 'Ravi', 'Sara', 'Meera', 'Arjun', 'Riya'}
Both days: {'Ahmed', 'Sara', 'Ravi'}
Monday only: {'Priya', 'Meera'}
One day only: {'Priya', 'Meera', 'Arjun', 'Riya'}
True
Truefrozenset — An Immutable Set
A frozenset is a set that cannot be changed after creation — like a tuple is to a list. Because it is hashable (immutable), it can be used as a dictionary key (regular sets cannot).
frozen = frozenset({"Python", "SQL", "Excel"}) # create from a set or list
print(frozen) # frozenset({'Python', 'SQL', 'Excel'})
# frozen.add("R") # AttributeError: 'frozenset' object has no attribute 'add'
# frozen.remove("SQL") # AttributeError — cannot modify a frozenset
# Use as a dictionary key (regular sets cannot be dict keys)
skill_groups = {
frozenset({"Python", "SQL"}): "Data Analyst",
frozenset({"Python", "ML"}): "ML Engineer",
}
query = frozenset({"Python", "SQL"})
print(skill_groups[query]) # Data Analyst
Output:
frozenset({'Python', 'SQL', 'Excel'})
Data Analyst