How to Work with Sets in Python
Learn how to create and use sets in Python for storing unique values and performing set operations.
Sets are unordered collections of unique elements in Python. They’re perfect for membership testing and eliminating duplicates.
Creating Sets
# Using curly braces
fruits = {"apple", "banana", "cherry"}
# Using set() constructor
numbers = set([1, 2, 3, 4, 5])
# Empty set (must use set(), not {})
empty = set() # {} creates an empty dict
# From string (unique characters)
chars = set("hello")
print(chars) # {'h', 'e', 'l', 'o'}
Basic Operations
Adding and Removing
fruits = {"apple", "banana"}
# Add single element
fruits.add("cherry")
print(fruits) # {'apple', 'banana', 'cherry'}
# Add multiple elements
fruits.update(["mango", "grape"])
print(fruits) # {'apple', 'banana', 'cherry', 'mango', 'grape'}
# Remove (raises error if not found)
fruits.remove("banana")
# Discard (no error if not found)
fruits.discard("pear") # No error
# Pop (remove and return arbitrary element)
item = fruits.pop()
print(item)
# Clear all elements
fruits.clear()
Membership Testing
fruits = {"apple", "banana", "cherry"}
# Check if element exists
print("apple" in fruits) # True
print("mango" in fruits) # False
print("mango" not in fruits) # True
Set Operations
Union
Combine all elements:
a = {1, 2, 3}
b = {3, 4, 5}
# Using union() method
print(a.union(b)) # {1, 2, 3, 4, 5}
# Using | operator
print(a | b) # {1, 2, 3, 4, 5}
# Original sets unchanged
print(a) # {1, 2, 3}
Intersection
Elements in both sets:
a = {1, 2, 3}
b = {2, 3, 4}
# Using intersection() method
print(a.intersection(b)) # {2, 3}
# Using & operator
print(a & b) # {2, 3}
Difference
Elements in first set but not second:
a = {1, 2, 3, 4}
b = {3, 4, 5}
# Elements in a but not in b
print(a.difference(b)) # {1, 2}
print(a - b) # {1, 2}
# Elements in b but not in a
print(b.difference(a)) # {5}
print(b - a) # {5}
Symmetric Difference
Elements in either set, but not both:
a = {1, 2, 3}
b = {2, 3, 4}
print(a.symmetric_difference(b)) # {1, 4}
print(a ^ b) # {1, 4}
Set Comparisons
a = {1, 2, 3}
b = {1, 2, 3, 4, 5}
c = {1, 2, 3}
# Subset (all elements in other set)
print(a.issubset(b)) # True
print(a <= b) # True
print(a < b) # True (proper subset)
# Superset
print(b.issuperset(a)) # True
print(b >= a) # True
# Equality
print(a == c) # True
# Disjoint (no common elements)
d = {6, 7, 8}
print(a.isdisjoint(d)) # True
In-Place Modifications
a = {1, 2, 3}
b = {3, 4, 5}
# In-place union
a.update(b) # a = a | b
print(a) # {1, 2, 3, 4, 5}
# In-place intersection
a = {1, 2, 3}
a.intersection_update(b) # a = a & b
print(a) # {3}
# In-place difference
a = {1, 2, 3}
a.difference_update(b) # a = a - b
print(a) # {1, 2}
# In-place symmetric difference
a = {1, 2, 3}
a.symmetric_difference_update(b) # a = a ^ b
print(a) # {1, 2, 4, 5}
Frozen Sets
Immutable sets:
# Create frozen set
frozen = frozenset([1, 2, 3])
# Can't modify
# frozen.add(4) # Error!
# Can use as dictionary key or set element
d = {frozen: "value"}
nested = {frozenset([1, 2]), frozenset([3, 4])}
Practical Examples
Remove Duplicates
# Remove duplicates from list
numbers = [1, 2, 2, 3, 3, 3, 4]
unique = list(set(numbers))
print(unique) # [1, 2, 3, 4]
# Preserve order (Python 3.7+)
unique_ordered = list(dict.fromkeys(numbers))
print(unique_ordered) # [1, 2, 2, 3, 3, 3, 4] -> [1, 2, 3, 4]
Find Common Elements
list1 = ["apple", "banana", "cherry"]
list2 = ["banana", "cherry", "date"]
common = set(list1) & set(list2)
print(common) # {'banana', 'cherry'}
Track Seen Items
def has_duplicates(items):
seen = set()
for item in items:
if item in seen:
return True
seen.add(item)
return False
print(has_duplicates([1, 2, 3, 4])) # False
print(has_duplicates([1, 2, 3, 2])) # True
Filter Allowed Values
allowed_statuses = {"active", "pending", "completed"}
def validate_status(status):
return status in allowed_statuses
# Filter list
items = [
{"name": "Item 1", "status": "active"},
{"name": "Item 2", "status": "invalid"},
{"name": "Item 3", "status": "pending"}
]
valid_items = [item for item in items if item["status"] in allowed_statuses]
Count Unique Words
text = "the quick brown fox jumps over the lazy dog"
words = text.split()
unique_words = set(words)
print(f"Total words: {len(words)}") # 9
print(f"Unique words: {len(unique_words)}") # 8
Summary
- Sets store unique elements only
- Use
set()for empty sets,{}for non-empty - Operations:
union(|),intersection(&),difference(-),symmetric_difference(^) - Use
infor fast membership testing (O(1)) - Use
frozensetfor immutable sets - Great for removing duplicates and set mathematics