Python Sets
In Python, a set is an unordered collection of unique elements. While sets themselves are mutable, they can only store immutable (hashable) items.
Creating a Set
To create a set, you put the items inside curly braces {} and separate them with commas.
For example:
# Set of integers
numbers = {1, 2, 3, 4}
# Set of strings
fruits = {"apple", "banana", "mango", "orange"}
# Set of mixed data types
mixed = {"james", 30, True}
You can also create a set from an iterable like a list, tuple, or dictionary by using the set() constructor.
my_list = [1, 2, 2, 3, 4]
my_set = set(my_list)
print(my_set) # Output: {1, 2, 3, 4}
In this example, the list my_list contains the number 2 twice. When we convert this list into a set using set(my_list), all duplicates elements are automatically removed since sets can only store unique values. As a result, my_set includes each number from the original list only once. Hence, the resulting set is {1, 2, 3, 4}.
Creating an Empty Set
You can create an empty set by calling the set() constructor without any arguments. Using {} creates an emtpy dictionary, not a set.
For example:
empty = set()
Accessing Set Items
In Python, sets are unordered, so you cannot access their elements by index as you would with a list or tuple.
Adding Items to a Set
You can add items ot a set using either the add() or the update() method.
Add a Single Item
You can add a single item to a set using the add() method.
For example:
fruits = {"apple", "banana", "mango"}
fruits.add("orange")
print(fruits) # Output: {'mango', 'orange', 'banana', 'apple'}
Sets in Python are unordered, so the elements can appear in a different order each time you print the set.
Add Multiple Items
You can add multiple items items from an iterable like a list, tuple, or another set using the update() method.
For example:
fruits = {"apple", "banana", "mango"}
fruits.update(["orange", "pineapple"])
print(fruits) # Output: {'banana', 'orange', 'mango', 'apple', 'pineapple'}
Removing Items From a Set
There are several ways to remove items from a set, depending on what you need to do.
Using remove()
Removes a specific element from the set. Raises a KeyError if the element is not found.
For example:
fruits = {"apple", "banana", "mango", "orange"}
fruits.remove("apple")
print(fruits) # Output: {'banana', 'orange', 'mango'}
# fruits.remove("pineapple") # Raises KeyError
Using discard()
Removes a specific element from the set without raising an error if the element is not found. This makes it safer than remove() method.
For example:
fruits = {"apple", "banana", "mango", "orange"}
fruits.discard("banana")
fruits.discard("pineapple") # No error
print(fruits) # Output: {'orange', 'mango', 'apple'}
Using pop()
Removes and returns an arbitrary element from the set (since sets are unordered, you don’t know which one).
For example:
fruits = {"apple", "banana", "mango", "orange"}
removed = fruits.pop()
print(removed) # Output e.g.: apple
print(fruits) # Output: remaining elements
Using clear()
Removes all elements from the set.
For example:
fruits = {"apple", "banana", "mango", "orange"}
fruits.clear()
print(fruits) # Output: set()
Mathematical Set Operations
You can perform mathematic operations on sets, such as union, intersection, difference, and symmetric difference.
Union (| or union())
Combines all elements from both sets, removing duplicates.
For exmple:
a = {1, 2, 3}
b = {3, 4, 5}
# Using |
c = a | b
print(c) # Output: {1, 2, 3, 4, 5}
# Using union()
d = a.union(b)
print(d) # Output: {1, 2, 3, 4, 5}
Intersection (& or intersection())
Keeps only the elements that are common to both sets.
For example:
a = {1, 2, 3}
b = {2, 3, 4}
# Using &
c = a & b
print(c) # Output: {2, 3}
# Using intersection()
d = a.intersection(b)
print(d) # Output: {2, 3}
Difference (- or difference())
Keeps only the elements that are in the first set but not in the second.
For example:
a = {1, 2, 3}
b = {2, 3, 4}
# Using -
c = a - b
print(c) # Output: {1}
# Using difference()
d = a.difference(b)
print(d) # Output: {1}
Symmetric Difference (^ or symmetric_difference())
Keeps elements that are in either set but not in both.
For example:
a = {1, 2, 3}
b = {2, 3, 4}
# Using ^
c = a ^ b
print(c) # Output: {1, 4}
# Using difference()
d = a.symmetric_difference(b)
print(d) # Output: {1, 4}
Iterating Through a Set
You can iterate through a set using a for loop.
For example:
fruits = {"apple", "banana", "mango", "orange"}
for fruit in fruits:
print(fruit)
Output:
mango
orange
apple
banana
Set Comprehensions
Set comprehensions offer a concise and efficient way to create sets from existing iterables (such as lists, tuples, strings, or ranges) while optionally filtering elements based on a condition.
The basic syntax of set comprehension is:
{expression for item in iterable if condition}
Where,
expression: The value or operation applied to each item to include in the set.
item: The variable representing each element from the iterable during iteration.
condition (Optional): A filter that determines whether an item is included in the set.
Example: Create a set of squares
squares = {num ** 2 for num in range(10)}
print(squares) # Output: {0, 1, 64, 4, 36, 9, 16, 49, 81, 25}
Example: Create a set of even squares
squares = {num ** 2 for num in range(10) if num % 2 == 0}
print(squares) # Output: {0, 64, 4, 36, 16}