A set in Python is an unordered, mutable, and unindexed collection of unique elements. Sets are useful when you need to store distinct values and perform mathematical set operations like union, intersection, and difference.
- Unordered: Elements do not have a fixed position.
- Mutable: Elements can be added or removed.
- Unindexed: Cannot be accessed using an index.
- Unique Elements: No duplicates allowed.
- Heterogeneous Elements: Can store different data types.
- Create a Set
- Add elements using
add()orupdate() - Remove elements using
remove(),discard(), orpop() - Perform set operations like union, intersection, and difference
- Check relationships using
issubset(),issuperset(), andisdisjoint() - Use frozen sets for immutable sets if needed
# Using curly braces
my_set = {1, 2, 3, 4, 5}
# Using set() constructor
another_set = set([10, 20, 30])
# Creating an empty set
empty_set = set()my_set = {1, 2, 3}
# Add a single element
my_set.add(4)
# Add multiple elements
my_set.update([5, 6, 7])my_set = {1, 2, 3, 4, 5}
# Remove an element
my_set.remove(3) # Raises error if element is absent
# Discard an element
my_set.discard(2) # No error if element is absent
# Remove and return a random element
my_set.pop()
# Clear all elements
my_set.clear()A = {1, 2, 3}
B = {3, 4, 5}
print(A | B) # {1, 2, 3, 4, 5}print(A & B) # {3}print(A - B) # {1, 2}print(A ^ B) # {1, 2, 4, 5}A = {1, 2}
B = {1, 2, 3, 4}
print(A.issubset(B)) # True
print(B.issuperset(A)) # True
print(A.isdisjoint({5, 6})) # TrueA = frozenset([1, 2, 3, 4])
# A.add(5) # Error: 'frozenset' object has no attribute 'add'- Removing duplicates from a list
numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = set(numbers)- Finding common elements between lists
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
common = set(list1) & set(list2)- Set operations in data analysis
students_A = {"Alice", "Bob", "Charlie"}
students_B = {"Charlie", "David", "Edward"}
both_classes = students_A | students_B