Sets in python
Sets in Python are unordered collections of unique elements. They are a powerful tool for working with data when the speed of checking whether an item belongs to a collection and the absence of duplicates is important. They have a number of useful properties and methods that make it possible to efficiently perform mathematical operations on data, such as union, intersection, difference, and others.
Main characteristics of sets
- Unique elements: There can be no duplicate elements in the set. If you try to add an element that already exists, the array simply won't change. This is their key feature, ideal for removing duplicates.
- Disorder: The elements of the set do not have a specific order. This means that you cannot access an element by index (for example,
my_set[0]), as is done in lists. When iterating over a set, the order of the elements is not guaranteed.
- Changeability: You can add and remove multiple items. However, the elements of the set themselves must be immutable (hashed) objects. These can be numbers, strings, tuples. You cannot add mutable objects to a set, such as lists or other sets, because their contents may change, which violates the internal logic of sets.
Creating sets
Empty set
To create an empty set , be sure to use the set() function, because the syntax {} creates an empty dictionary. This is a common mistake of novice programmers.
# Correct
my_set = set()
# Wrong (this will create a dictionary)
my_dict = {}
A set with elements
You can create a set by listing the elements in curly brackets or by passing an iterable object (such as a list) to the set() function..
my_set = {1, 2, 3, 4, 5}
A useful tip: Using the set() functionThis is the easiest way to remove duplicates from the list.
my_list = [1, 2, 2, 3, 4, 3, 5, 5]
unique_elements = set(my_list)
print(unique_elements) # Output: {1, 2, 3, 4, 5}
Basic operations with sets
Adding elements
To add one element, use the add() method.
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}
# Trying to add an existing element will not change
anything my_set.add(2)
print(my_set) # Output: {1, 2, 3, 4}
Deleting elements
There are three ways to delete:
remove(element): Deletes the specified element. If the element is not found, it causes the KeyError error. Use this method when you are sure that an element should exist, and its absence is an error in the program logic.
discard(element): Deletes the specified element, but does not cause an error if the element is not found. This is a safe way to delete when you are not sure if an item is in the set.
pop(): deletes and returns an arbitrary element of the set. Since the sets are unordered, you cannot predict which element will be deleted. The method will cause a KeyError if the set is empty.
my_set = {1, 2, 3}
my_set.remove(2)
print(my_set) # Output: {1, 3}
# my_set.remove(4) # This will cause a KeyError error
my_set.discard(4) # There will be no error, the set will remain {1, 3}
popped_element = my_set.pop()
print(popped_element) # Output: 1 or 3 (depends on implementation)
print(my_set) # Output: remaining element
Clearing the set
The clear() method removes all elements from the set, making it empty.
my_set = {1, 2, 3}
my_set.clear()
print(my_set) # Output: set()
Operations on sets
These operations do not change the original sets, but create and return a new one.
Union
Returns a new set containing all the unique elements from both sets. The | operator or the union() method is used. When to use: When you need to collect all the unique items from multiple collections into one.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2
# or
union_set = set1.union(set2)
print(union_set) # Output: {1, 2, 3, 4, 5}
Intersection
Returns a new set containing only the elements that are in both sets. The & operator or the intersection() method is used. When to use: When you need to find common elements between two groups.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1 & set2
# or
intersection_set = set1.intersection(set2)
print(intersection_set) # Output: {3}
Difference
Returns a new set containing the elements that are in the first set but are missing from the second. The - operator or the difference() method is used. When to use: When you need to find what is in one group but not in another. The order is important: set1 - set2 is not the same as set2 - set1.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1 - set2
print(difference_set) # Output: {1, 2}
difference_set_2 = set2 - set1
print(difference_set_2) # Output: {4, 5}
Symmetrical Difference
Returns a new set containing elements that are in one of the sets, but not both at once. The ^ operator or the symmetric_difference() method is used. When to use: When you need to find elements unique to each group, excluding the common ones.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_difference_set = set1 ^ set2
# or
symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set) # Output: {1, 2, 4, 5}