Python Sets - A Comprehensive Guide
Overview
A set in Python is an unordered collection of unique items. It is a powerful data structure for scenarios where uniqueness of elements is required, such as removing duplicates or performing mathematical set operations like union, intersection, and difference.
Key Characteristics
- Unordered: Elements have no specific order, and indexing is not supported.
- Unique Items: No duplicate elements are allowed.
- Mutable: Elements can be added or removed, but the set itself is mutable.
- Heterogeneous: Sets can store elements of different data types (e.g., integers, strings, tuples).
Creating Sets
Sets can be created using curly braces {}
or the set()
constructor.
Syntax
# Using curly braces
my_set = {1, 2, 3}
# Using the set() constructor
empty_set = set() # Note: {} creates an empty dictionary, not a set
string_set = set("hello")
Examples
# Creating a set
fruits = {"apple", "banana", "cherry"}
print(fruits) # Output: {'apple', 'banana', 'cherry'}
# Set from a list (removes duplicates)
numbers = set([1, 2, 2, 3, 4])
print(numbers) # Output: {1, 2, 3, 4}
Accessing Elements in a Set
Since sets are unordered, elements cannot be accessed by index. However, you can iterate over a set using a for
loop.
fruits = {"apple", "banana", "cherry"}
for fruit in fruits:
print(fruit)
Modifying Sets
Adding Elements
add(item)
: Adds a single element to the set.update(iterable)
: Adds multiple elements from an iterable to the set.
fruits = {"apple", "banana"}
fruits.add("cherry")
print(fruits) # Output: {'apple', 'banana', 'cherry'}
fruits.update(["date", "elderberry"])
print(fruits) # Output: {'apple', 'banana', 'cherry', 'date', 'elderberry'}
Removing Elements
remove(item)
: Removes the specified item; raisesKeyError
if the item does not exist.discard(item)
: Removes the specified item; does nothing if the item does not exist.pop()
: Removes and returns an arbitrary element.clear()
: Removes all elements from the set.
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits) # Output: {'apple', 'cherry'}
fruits.discard("pear") # No error if 'pear' does not exist
fruits.pop()
print(fruits) # Output: {'cherry'} (arbitrary element removed)
fruits.clear()
print(fruits) # Output: set()
Set Operations
Union
Combines elements from both sets (no duplicates).
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set) # Output: {1, 2, 3, 4, 5}
Intersection
Finds elements common to both sets.
intersection_set = set1.intersection(set2)
print(intersection_set) # Output: {3}
Difference
Finds elements in the first set but not in the second.
difference_set = set1.difference(set2)
print(difference_set) # Output: {1, 2}
Symmetric Difference
Finds elements in either set, but not in both.
symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set) # Output: {1, 2, 4, 5}
Built-in Functions and Methods
len(set)
: Returns the number of elements in the set.in
: Checks if an element exists in the set.isdisjoint(other_set)
: ReturnsTrue
if two sets have no common elements.issubset(other_set)
: Checks if the set is a subset of another.issuperset(other_set)
: Checks if the set is a superset of another.
set1 = {1, 2, 3}
set2 = {2, 3}
print(len(set1)) # Output: 3
print(2 in set1) # Output: True
print(set1.isdisjoint({4, 5})) # Output: True
print(set2.issubset(set1)) # Output: True
print(set1.issuperset(set2)) # Output: True
Tuples vs Lists vs Sets
Feature | Tuple | List | Set |
---|---|---|---|
Order | Ordered | Ordered | Unordered |
Mutability | Immutable | Mutable | Mutable (but elements must be immutable) |
Duplicates | Allowed | Allowed | Not allowed |
Indexing | Supported | Supported | Not supported |
Performance | Fast (fixed size) | Slower (dynamic resizing) | Fast (hashing) |
Usage | Fixed collections | Dynamic collections | Unique elements, set operations |
Conclusion
Python sets are an efficient and versatile data structure for scenarios requiring unique elements and set operations. Understanding their properties, methods, and use cases is essential for effective Python programming. Combined with tuples and lists, they provide a comprehensive toolkit for working with collections.