Python Sets
Sets are unordered collections of unique elements. They support mathematical set operations like union, intersection, and difference.
What are Sets?
Sets are unordered collections of unique, immutable elements. They automatically remove duplicates and provide fast membership testing. Sets are mutable but can only contain immutable elements.
"Sets are unordered collections of unique elements, perfect for mathematical operations."
Creating Sets
# Empty set
empty_set = set()
# Set with elements
fruits = {"apple", "banana", "cherry"}
numbers = {1, 2, 3, 4, 5}
# From list (removes duplicates)
duplicates = [1, 2, 2, 3, 3, 3]
unique_numbers = set(duplicates)
print(unique_numbers) # {1, 2, 3}
# From string
letters = set("hello")
print(letters) # {'h', 'e', 'l', 'o'}
# Note: {} creates dict, not set
not_a_set = {} # This is a dict
print(type(not_a_set)) # <class 'dict'>
Set Operations
fruits = {"apple", "banana", "cherry", "orange"}
vegetables = {"carrot", "potato", "tomato"}
# Adding elements
fruits.add("grape")
print(fruits)
# Removing elements
fruits.remove("banana") # Raises KeyError if not found
fruits.discard("kiwi") # No error if not found
popped = fruits.pop() # Removes random element
# Clear all elements
temp = {1, 2, 3}
temp.clear()
print(temp) # set()
# Membership testing
print("apple" in fruits) # True
print(len(fruits)) # 4
# Copy
fruits_copy = fruits.copy()
Mathematical Set Operations
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
# Union - elements in A or B or both
print(A | B) # {1, 2, 3, 4, 5, 6, 7, 8}
print(A.union(B)) # Same result
# Intersection - elements in both A and B
print(A & B) # {4, 5}
print(A.intersection(B)) # Same result
# Difference - elements in A but not in B
print(A - B) # {1, 2, 3}
print(A.difference(B)) # Same result
# Symmetric difference - elements in A or B but not both
print(A ^ B) # {1, 2, 3, 6, 7, 8}
print(A.symmetric_difference(B)) # Same result
# Subset and superset
C = {1, 2, 3}
D = {1, 2, 3, 4, 5}
print(C.issubset(D)) # True
print(D.issuperset(C)) # True
# Disjoint sets - no common elements
E = {1, 2}
F = {3, 4}
print(E.isdisjoint(F)) # True
Set Methods
Sets provide methods for in-place operations:
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, 4}
A.intersection_update(B) # A = A & B
print(A) # {3, 4}
# In-place difference
A = {1, 2, 3, 4}
A.difference_update(B) # A = A - B
print(A) # {1, 2}
# In-place symmetric difference
A = {1, 2, 3, 4}
A.symmetric_difference_update(B) # A = A ^ B
print(A) # {1, 2, 5}
Frozen Sets
Frozen sets are immutable versions of sets:
# Creating frozen sets
normal_set = {1, 2, 3}
frozen = frozenset([1, 2, 3])
print(type(frozen)) # <class 'frozenset'>
# Frozen sets are immutable
# frozen.add(4) # AttributeError
# Can be used as dict keys or set elements
my_dict = {frozen: "value"}
my_set = {frozenset([1, 2]), frozenset([3, 4])}
# Support all read-only operations
A = frozenset([1, 2, 3])
B = frozenset([3, 4, 5])
print(A | B) # frozenset({1, 2, 3, 4, 5})
Set Comprehensions
# Basic set comprehension
squares = {x**2 for x in range(1, 6)}
print(squares) # {1, 4, 9, 16, 25}
# Conditional comprehension
even_squares = {x**2 for x in range(1, 11) if x % 2 == 0}
print(even_squares) # {4, 16, 36, 64, 100}
# From string
vowels = {char for char in "hello world" if char in "aeiou"}
print(vowels) # {'o', 'e'}
# Nested comprehension
matrix = [[1, 2], [3, 4], [5, 6]]
flattened = {num for row in matrix for num in row}
print(flattened) # {1, 2, 3, 4, 5, 6}
Practical Applications
- Removing Duplicates: Convert list to set and back
- Membership Testing: Fast lookups for large datasets
- Mathematical Operations: Union, intersection for data analysis
- Data Deduplication: Ensuring unique values
- Set Algebra: Database operations, filtering
- Caching Unique Items: Tracking unique visitors, items
Performance Characteristics
| Operation | Time Complexity | Description |
|---|---|---|
| Add element | O(1) | Add new element |
| Remove element | O(1) | Remove existing element |
| Membership test | O(1) | Check if element exists |
| Union | O(len(A) + len(B)) | Combine two sets |
| Intersection | O(min(len(A), len(B))) | Find common elements |
| Difference | O(len(A)) | Find elements in A but not B |
Sets are powerful data structures for handling unique collections and performing mathematical operations. They're particularly useful when you need fast membership testing or want to eliminate duplicates automatically.