Master the Building Blocks of Efficient Code
A set is an unordered collection of unique elements. Sets are mutable, meaning you can add or remove elements from them. They are particularly useful for membership testing, removing duplicates from a sequence, and performing mathematical set operations like union, intersection, difference, and symmetric difference.
You can create a set using curly braces {} or the set() constructor.
# Using curly braces
my_set1 = {1, 2, 3, 4, 5}
print(my_set1) # Output might be {1, 2, 3, 4, 5} (order may vary)
# Using the set() constructor with a list
my_set2 = set([1, 2, 2, 3, 4, 4, 4])
print(my_set2) # Output: {1, 2, 3, 4} (duplicates removed)
# Creating an empty set (Note: {} creates an empty dictionary)
empty_set = set()
print(empty_set) # Output: set()
Python provides several built-in methods and operators for set manipulation.
add(element): Adds a single element to the set. If the element is already present, the set remains unchanged.my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}
my_set.add(2) # Adding an existing element
print(my_set) # Output: {1, 2, 3, 4}
remove(element): Removes the specified element. Raises a KeyError if the element is not found.discard(element): Removes the specified element if it is present. Does not raise an error if the element is not found.pop(): Removes and returns an arbitrary element from the set. Raises a KeyError if the set is empty.clear(): Removes all elements from the set.my_set = {1, 2, 3, 4, 5}
my_set.remove(3)
print(my_set) # Output: {1, 2, 4, 5}
# my_set.remove(10) # This would raise a KeyError
my_set.discard(4)
print(my_set) # Output: {1, 2, 5}
my_set.discard(10) # No error raised
print(my_set) # Output: {1, 2, 5}
popped_element = my_set.pop()
print(f"Popped element: {popped_element}")
print(my_set) # my_set will have one less element
my_set.clear()
print(my_set) # Output: set()
Let A = {1, 2, 3, 4} and B = {3, 4, 5, 6}.
A.union(B)A | BA.intersection(B)A & BA.difference(B) (for A - B)A - BA.symmetric_difference(B)A ^ BA = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print(f"Union (A | B): {A | B}") # Output: {1, 2, 3, 4, 5, 6}
print(f"Intersection (A & B): {A & B}") # Output: {3, 4}
print(f"Difference (A - B): {A - B}") # Output: {1, 2}
print(f"Difference (B - A): {B - A}") # Output: {5, 6}
print(f"Symmetric Difference (A ^ B): {A ^ B}") # Output: {1, 2, 5, 6}
You can check if an element exists in a set using the in keyword. This is a very efficient operation for sets (average O(1) time complexity).
my_set = {"apple", "banana", "cherry"}
print("apple" in my_set) # Output: True
print("grape" in my_set) # Output: False
# Removing duplicates from a list
my_list = [1, 1, 2, 3, 4, 4, 5, 2]
unique_list = list(set(my_list))
print(unique_list) # Output: [1, 2, 3, 4, 5] (order may vary)
Sets are a powerful and efficient data structure in Python for managing collections of unique items and performing common mathematical set operations. Applications like autonomous agents for portfolio analysis use sets to track unique securities and perform rapid intersection/union operations on market data.