Python Sets
Sets in Python are a powerful and versatile data type used to store unique, unordered elements.
They are highly optimized for operations like membership testing, union, intersection, and difference.
What is a Set?:
- A set is an unordered collection of unique elements.
- Sets are mutable (can be changed).
- Sets do not allow duplicates.
- Sets are written with curly braces {} (like dictionaries, but without key-value pairs).
Example:
fruits = {"apple", "banana", "cherry"}
print(fruits) # Output: {'banana', 'apple', 'cherry'} (order may vary)
Creating a Set:
You can create a set in two ways:
- Using curly braces { }
- Using the set( ) constructor.
Example:
# Method 1: Using curly braces
colors = {"red", "green", "blue"}
# Method 2: Using set()
numbers = set([1, 2, 3, 4])
# Empty set (Note: { } creates a dictionary, not a set)
empty_set = set( )
Set Operations
Sets support various operations to manipulate and query the data they contain:
- Add Elements: Use the
add() method to add a single element.
- Update Elements: Use the
update() method to add multiple elements.
- Remove Elements: Use the
remove() method to remove a specific element.
- Union: Combine two sets using the
union() method or the | operator.
- Intersection: Get common elements using the
intersection() method or the & operator.
- Difference: Get elements in one set but not the other using the
difference() method or the - operator.
Example:
# Adding elements
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits) # Output: {'banana', 'apple', 'cherry', 'orange'} (order may vary)
# Updating elements
fruits.update(["kiwi", "grape"])
print(fruits) # Output: {'banana', 'apple', 'cherry', 'orange', 'kiwi', 'grape'} (order may vary)
# Removing elements
fruits.remove("banana")
print(fruits) # Output: {'apple', 'cherry', 'orange', 'kiwi', 'grape'} (order may vary)
# Union of sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set_union = set1.union(set2)
print(set_union) # Output: {1, 2, 3, 4, 5}
# Intersection of sets
set_intersection = set1.intersection(set2)
print(set_intersection) # Output: {3}
# Difference of sets
set_difference = set1.difference(set2)
print(set_difference) # Output: {1, 2}
Removing Elements:
- remove( ): Removes an element (raises error if not found).
- discard( ): Removes an element (no error if not found).
- pop( ): Removes & returns a random element.
- clear( ): Removes all elements.(Empties the set.)
Example:
s = {1, 2, 3, 4}
s.remove(3) # {1, 2, 4}
s.discard(10) # No error
s.pop() # Removes random element (e.g., 1)
s.clear() # set()
Checking Membership:
You can check if an element is in a set using the in keyword.
Example:
fruits = {"apple", "banana"}
print("apple" in fruits) # True
print("mango" in fruits) # False
Set Methods:
| Method |
Description |
add( ) |
Adds an element to the set |
remove( ) |
Removes an element; raises error if not found |
discard( ) |
Removes an element if present (no error if not found) |
pop( ) |
Removes and returns a random element |
clear( ) |
Removes all elements from the set |
update( ) |
Adds elements from another set or iterable |
union( ) |
Returns a new set with all elements from both sets |
intersection( ) |
Returns a set with common elements |
difference( ) |
Returns a set with elements not in another set |
copy( ) |
Returns a shallow copy of the set |
symmetric_difference( ) |
Returns elements in either set, but not both |
Frozen Sets (Immutable Sets):
- Frozen sets are immutable (cannot be changed after creation).
- Created using frozenset( ).
Example:
fs = frozenset([1, 2, 3])
# fs.add(4) → Error (frozen sets are immutable)
Set Operations (Union, Intersection, Difference):
Union ( | or union( ) )
Combines elements from both sets.
Example:
A = {1, 2, 3}
B = {3, 4, 5}
print(A | B) # {1, 2, 3, 4, 5}
print(A.union(B)) # Same output
Intersection ( & or intersection( ) )
Returns common elements.
Example:
print(A & B) # {3}
print(A.intersection(B)) # Same output
Difference ( - or difference( ) )
Returns elements in A but not in B.
Example:
print(A - B) # {1, 2}
print(A.difference(B)) # Same output
Symmetric Difference ( ^ or symmetric_difference( ) )
Returns elements in either set but not both.
Example:
print(A ^ B) # {1, 2, 4, 5}
Practical Use Cases:
- Removing duplicates from a list.
- Membership testing (faster than lists).
- Mathematical operations (union, intersection).
Example:
# Remove duplicates from a list
names = ["Alice", "Bob", "Alice", "Charlie"]
unique_names = set(names) # {'Alice', 'Bob', 'Charlie'}
Tricky Concepts & Common Mistakes:
1. Sets are Unordered
- Sets do not maintain insertion order.
Example:
s = {3, 1, 2}
print(s) # May print {1, 2, 3} or any order
2. Sets Cannot Contain Mutable Elements
- You cannot have lists or dictionaries inside a set.
Example:
# invalid_set = {1, [2, 3]} → Error (unhashable type: 'list')
valid_set = {1, (2, 3)} # Tuples are allowed (immutable)
3. pop( ) Removes a Random Element
- Since sets are unordered, pop( ) removes an arbitrary element.
Example:
s = {5, 10, 15}
print( s.pop( ) ) # Could be 5, 10, or 15
4. { } is Not an Empty Set!
- { } creates an empty dictionary, not a set.
Example:
5. len( ) - Finding the Length of a Set
- len(set) returns the number of elements in a set.
- Since sets store only unique elements, duplicates are not counted.
Example:
fruits = {"apple", "banana", "cherry", "apple"}
print(len(fruits)) # Output: 3 (because "apple" is duplicate)
6. type( ) - Checking Data Type of a Set
- type( ) helps identify if a variable is a set, list, tuple, etc.
- Useful for debugging and type-checking.
Example:
fruits = {"apple", "banana", "cherry", "apple"}
print(len(fruits)) # Output: 3 (because "apple" is duplicate)