PYTHON
Performing Efficient Set Operations with Python's set Data Structure
Leverage Python's set for managing unique elements and performing fast mathematical set operations like union, intersection, and difference, crucial for data processing.
# Create sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
set3 = set([3, 8, 9]) # From a list
print(f"Set 1: {set1}")
print(f"Set 2: {set2}")
print(f"Set 3: {set3}")
# Union (elements in either set)
union_set = set1.union(set2) # or set1 | set2
print(f"Union of set1 and set2: {union_set}")
# Intersection (elements common to both sets)
intersection_set = set1.intersection(set2) # or set1 & set2
print(f"Intersection of set1 and set2: {intersection_set}")
# Difference (elements in set1 but not in set2)
difference_set = set1.difference(set2) # or set1 - set2
print(f"Difference (set1 - set2): {difference_set}")
# Symmetric Difference (elements in either set, but not in both)
symmetric_difference_set = set1.symmetric_difference(set2) # or set1 ^ set2
print(f"Symmetric Difference (set1 ^ set2): {symmetric_difference_set}")
# Check for subset and superset
is_subset = {1, 2}.issubset(set1)
is_superset = set1.issuperset({1, 2})
print(f"{{1, 2}} is a subset of set1: {is_subset}")
print(f"set1 is a superset of {{1, 2}}: {is_superset}")
# Add and remove elements
set1.add(6)
set1.remove(1) # Raises KeyError if element not present
set1.discard(10) # Doesn't raise error if element not present
print(f"Set1 after add/remove/discard: {set1}")
How it works: Python's set is an unordered collection of unique elements. It provides highly optimized methods for performing mathematical set operations like union, intersection, difference, and symmetric difference. Sets are mutable, allowing elements to be added or removed, and are useful for tasks requiring unique item storage, membership testing, and efficient comparisons between collections, often completing these tasks in average O(1) time. This makes them invaluable for filtering data or comparing collections of unique identifiers.