Python Data Structures: Sets 🔥

A set in Python is an unordered, mutable collection of unique elements. Sets are useful when you need to store items without duplicates and perform operations like union, intersection, and difference efficiently.
1️⃣ Creating Sets
🔹 Basic Set Creation
Sets are created using curly braces {} or the set() constructor.
my_set = {1, 2, 3, 4, 5}
print(my_set) # Output: {1, 2, 3, 4, 5}
empty_set = set() # ✅ Correct way to create an empty set
not_a_set = {} # ❌ Creates an empty dictionary, not a set
🔹 Set with Duplicates (Duplicates Are Removed)
numbers = {1, 2, 2, 3, 4, 4, 5}
print(numbers) # Output: {1, 2, 3, 4, 5}
🔹 Creating a Set from a List
my_list = [1, 2, 3, 3, 4, 5, 5]
my_set = set(my_list)
print(my_set) # Output: {1, 2, 3, 4, 5}
2️⃣ Adding & Removing Elements
Method Description
add(item) Adds an element
remove(item) Removes an element (raises KeyError if not found)
discard(item) Removes an element (does not raise an error)
pop() Removes and returns a random element
clear() Removes all elements
🔹 Adding Elements
fruits = {"apple", "banana"}
fruits.add("cherry")
print(fruits) # Output: {'apple', 'banana', 'cherry'}
🔹 Removing Elements
fruits.remove("banana") # ✅ Removes 'banana'
# fruits.remove("grape") # ❌ KeyError if 'grape' is not in the set
fruits.discard("grape") # ✅ No error if 'grape' is not found
print(fruits.pop()) # Removes and returns a random element
fruits.clear() # Empty the set
print(fruits) # Output: set()
3️⃣ Set Operations (Union, Intersection, Difference, Symmetric Difference)
Operation Symbol Method
Union (combines elements) `A B`
Intersection (common elements) A & B A.intersection(B)
Difference (elements in A but not in B) A - B A.difference(B)
Symmetric Difference (elements in A or B but not both) A ^ B A.symmetric_difference(B)
🔹 Examples
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print(A | B) # Union: {1, 2, 3, 4, 5, 6}
print(A & B) # Intersection: {3, 4}
print(A - B) # Difference: {1, 2} (elements in A but not in B)
print(A ^ B) # Symmetric Difference: {1, 2, 5, 6} (elements in A or B, but not both)
4️⃣ Checking Membership & Subsets
Operation Symbol Method
Check if element exists in x in A
Check subset A <= B A.issubset(B)
Check superset A >= B A.issuperset(B)
Check disjoint sets A.isdisjoint(B) No common elements
🔹 Examples
A = {1, 2, 3}
B = {1, 2, 3, 4, 5}
print(2 in A) # Output: True
print(A.issubset(B)) # Output: True
print(B.issuperset(A)) # Output: True
print(A.isdisjoint({4, 5})) # Output: True (no common elements)
5️⃣ Frozen Sets (Immutable Sets)
A frozen set is an immutable version of a set. Once created, it cannot be modified (no add or remove operations).
frozen = frozenset([1, 2, 3, 4])
# frozen.add(5) # ❌ AttributeError: 'frozenset' object has no attribute 'add'
print(frozen)
6️⃣ When to Use Sets?
✅ When you need unique elements (e.g., removing duplicates).
✅ When performing set operations (union, intersection, etc.).
✅ When you want fast lookups (checking membership is faster than lists).
Date: 2025-03-22 00:00:00.000000