A set in Python is an unordered, mutable, and unindexed collection of unique elements. Sets are commonly used when you need to store distinct values and perform operations like union, intersection, and difference.
Key Features of Sets
- Unordered: Sets do not maintain the order of their elements.
- Unique Elements: Duplicate elements are automatically removed.
- Mutable: Sets can be modified (e.g., adding or removing elements).
- Unindexed: You cannot access elements by index.
- Optimized Operations: Sets are optimized for membership tests (e.g.,
in
ornot in
).
Creating Sets
You can create a set by enclosing elements in curly braces {}
or using the set()
constructor.
# Empty set (must use set(), not {})
empty_set = set()
# Set with integers
int_set = {1, 2, 3}
# Set with mixed data types
mixed_set = {1, "hello", 3.14}
# Set with duplicate elements (duplicates are removed automatically)
unique_set = {1, 2, 2, 3, 3, 3}
print(unique_set) # Output: {1, 2, 3}
Accessing Elements
Since sets are unordered, you cannot access elements by index. However, you can iterate through a set using a loop.
fruits = {"apple", "banana", "cherry"}
# Iterating through a set
for fruit in fruits:
print(fruit)
# Checking membership
print("apple" in fruits) # Output: True
print("grape" in fruits) # Output: False
Set Methods
Method | Description | Example |
---|---|---|
add() | Adds an element to the set | s.add(5) |
remove() | Removes an element (raises error if not found) | s.remove(2) |
discard() | Removes an element (does not raise error if not found) | s.discard(10) |
pop() | Removes and returns an arbitrary element | s.pop() |
clear() | Removes all elements from the set | s.clear() |
copy() | Returns a shallow copy of the set | new_set = s.copy() |
update() | Adds elements from another set or iterable | s.update([4, 5]) |
union() | Returns a new set with elements from both sets | s1.union(s2) |
intersection() | Returns a set with elements common to both sets | s1.intersection(s2) |
difference() | Returns a set with elements in one set but not the other | s1.difference(s2) |
symmetric_difference() | Returns a set with elements in either set but not both | s1.symmetric_difference(s2) |
issubset() | Checks if a set is a subset of another | s1.issubset(s2) |
issuperset() | Checks if a set is a superset of another | s1.issuperset(s2) |
isdisjoint() | Checks if two sets have no elements in common | s1.isdisjoint(s2) |
Basic Set Operations
Adding Elements
numbers = {1, 2, 3}
numbers.add(4)
print(numbers) # Output: {1, 2, 3, 4}
Removing Elements
numbers = {1, 2, 3}
numbers.remove(2) # Removes 2
# numbers.remove(5) # Raises KeyError if element not found
numbers.discard(5) # Does not raise an error if element not found
print(numbers) # Output: {1, 3}
Union
Combines all unique elements from two sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2)) # Output: {1, 2, 3, 4, 5}
Intersection
Finds common elements between two sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.intersection(set2)) # Output: {3}
Difference
Finds elements in one set but not in the other.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.difference(set2)) # Output: {1, 2}
Symmetric Difference
Finds elements in either set but not both.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.symmetric_difference(set2)) # Output: {1, 2, 4, 5}
Frozen Sets
A frozenset is an immutable version of a set. You cannot modify its elements after creation.
fs = frozenset([1, 2, 3, 3])
print(fs) # Output: frozenset({1, 2, 3})
# fs.add(4) # Raises AttributeError: 'frozenset' object has no attribute 'add'
Example: Removing Duplicates Using Sets
# Removing duplicates from a list
numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = set(numbers)
print(unique_numbers) # Output: {1, 2, 3, 4, 5}
Use Cases of Sets
- Membership Testing: Quickly check if an item exists in a collection.
- Removing Duplicates: Convert a list to a set to eliminate duplicates.
- Mathematical Operations: Perform union, intersection, and difference for data analysis.
Comparison with Lists and Tuples
Feature | Set | List | Tuple |
---|---|---|---|
Ordered | No | Yes | Yes |
Mutable | Yes | Yes | No |
Duplicates | Not allowed | Allowed | Allowed |
Indexed Access | No | Yes | Yes |
Conclusion
Python sets are versatile for working with unique collections of data and performing efficient mathematical operations. Their unordered and unique nature makes them an excellent choice for tasks involving membership testing, data deduplication, and set-based operations.