Python Basics
Collections - Sets
A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.
The set type is mutable — the contents can be changed using methods like
set.add
and set.remove
. Since it is mutable, it has no hash value and cannot be used
as either a dictionary key or as an element of another set.
Tristan
contact-datartichaut@pm.me
Python projects list
Beginner
What are sets and why use them.
In more technical termes, sets are defined as :
Sets are a bit harder to work with because they don't allow duplicate elements and being unordered prevent
you to use the really common collection[0]
to select element into it.
The real power with sets is in all the operations that can be done with sets like union, intersection and so on. It can be extremely useful in cases where you have to get every elements that are in a collection but not in another.
ToC
s = {}
s = set()
They can be initialized directly with values.
s = {1, 2, 3, 4}
s = set([1, 2, 3, 4])
Beginner
Accessing elements in a set.
Sets are not ordered. Hence you cannot acces values by index or slice a set, since these notions do not exist regarding sets. They are mainly handled with operations on one or more sets.
ToC
Beginner
Adding elements to a set.
There is one simple way to add elements to a set, the set.add
method.
ToC
s1 = {1, 2}
s1.add(3)
print(s1)
The set.update
method can also be used and can handle collections.
s1.update([4, 5])
print(s1)
Unions can be considered a way to add elements to a dictionary.
The difference is it requires two dictionaries and creates a third one.
Beginner
Modifying elements in a set.
Since sets are unordered, modifying a specific items does not make much sense.
It is more frequent to delete elements and add new ones.
ToC
Beginner
Deleting elements from a set.
Unlike accessing elements, sets provide several interesting ways to remove elements.
ToC
s1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
The set.remove
method removes an element from the set and raises a KeyError if
the element is not contained in the set.
print("Removing an item by value :")
s1.remove(2)
print('\nState of the set :')
print(s1)
The set.discard
method removes an element from the set if it is present.
print("Discarding an item by value :")
s1.discard(2)
print('\nState of the set :')
print(s1)
The set.pop
method removes and return an arbitrary element from the set. Raises KeyError
if the set is empty.
print('Item return by pop :')
print(s1.pop())
print('\nState of the set :')
print(s1)
The set.clear
method removes all elements from the set.
print("Clearing the set.")
s1.clear()
print('\nState of the set :')
print(s1)
Beginner
Operations on sets.
Sets allow to use powerfull operation.
First we can declare a bunch of sets to illustrate all these operations.
ToC
# Sets of various length.
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
c = {5, 6, 7, 8}
d = {2, 3}
e = {0, 1, 2, 3, 4, 5}
Unions.
set.union(a, b)
# Or ...
a.union(b)
# Or ...
a | b
a = {1, 2}
b = {4, 5}
print(f"Union of {a} and {b}")
print("\nResults :")
print(f"{a | b}")
Intersections.
set.intersection(a, b)
# Or ...
a.intersection(b)
# Or ...
a & b
print(f"Intersection of {a} and {b}")
print("\nResults :")
print(f"{a & b}")
Subsets.
set.issubset(a, b)
# Or ...
a.issubset(b)
# Or ...
a < b
print(f"Is {a} a subset of {b}")
print("\nResults :")
print(f"{a < b}")
This operator is not symetrical.
print(f"{a} is a subset of {e}")
print(f"> a < e == {a < e}.")
print(f"\n but {e} is a subset of {a}")
print(f"> e < a == {e < a}.")
Supersets.
Same as subsets.
set.issuperset(a, b)
# Or ...
a.issuperset(b)
# Or ...
a > b
Difference.
set.difference(a, b)
# Or ...
a.difference(b)
# Or ...
a - b
print(f"Difference of {a} minus {b}")
print("\nResults :")
print(f"{a - b}")
Symetric difference.
set.symmetric_difference(a, b)
# Or ...
a.symmetric_difference(b)
# Or ...
a ^ b
print(f"Symetric difference of {a} and {b}")
print("\nResults :")
print(f"{a ^ b}")
Checking if sets are disjoint.
print('Is a disjointed from b :')
print(a.isdisjoint(b))
print('Is a disjointed from c :')
print(a.isdisjoint(c))
Advanced
What is the difference between adding an element to a dictionary and using the union operator ?
Let's create a set.
s1 = {1, 2, 3, 4}
Now let's add an element to this set and see if it is still the same object.
print(f"Address of the set before adding : {hex(id(s1))}.")
s1.add(5)
print(f"Address of the set after adding : {hex(id(s1))}.")
Now let's see what happens if we add an element with the union operator.
s2 = {9, 10, 11}
print(f"Address of the set before adding : {hex(id(s1))}.")
s1 = s1 | s2
print(f"Address of the set after adding : {hex(id(s1))}.")
s1 is not the same object anymore.
Note this does not happen with the |=
operator as it implicitely call
the set.update
method.
# A basic set.
s1 = {1, 2}
# Creating copies.
s2 = s1
s3 = s1.copy()
print(f"Address of s1 : {hex(id(s1))}")
print(f"Address of s2 : {hex(id(s2))}")
print(f"Address of s3 : {hex(id(s3))}")
Nested sets requiere the copy.deepcopy
function from the copy
library, same as lists.
Beginner
Iterating through a set.
Iterating through a set works in the exact same way as it does with lists.
Sets being unordered collection, iterating through them can yield unepexted results.
ToC
# A simple set.
s1 = {1, 4, 3, 2}
lst_cumsum = []
for ind, elt in enumerate(s1):
try:
lst_cumsum.append(lst_cumsum[ind - 1] + elt)
except IndexError:
lst_cumsum.append(elt)
print(lst_cumsum)
Given the set {1, 4, 3, 2}
. One might expect the cumulative sum to be :
[1, 5, 8 10]
But the result is :
[1, 3, 6, 10]
Beginner
Logic tests with sets.
Just like with lists, a set can be tested and is Truthy if not empty.
ToC
s1 = {1, 2, 3}
while s1:
elem = s1.pop()
print(f"Random element in our set : {elem}")
Beginner
Unpacking a set.
Same as before, sets being unordered, unpacking is possible but can have unexpected effects.
When iterating through a collection, the order may not matter but with unpacking it is almost never the case, therefore unpacking a set should be avoided.
ToC
a, b, c = {1, 3, 2}
print("Expected values : a -> 1, b -> 3, c -> 2")
print(f"Unpacked values : a -> {a}, b -> {b}, c -> {c}")
Intermediate
Set comprehension.
Set comprehension is possible, just the same as list comprehension.
Set comprehension can be used when the order of the result does not matter and when we do not want to know duplicated results.
ToC
# A list of number.
s1 = {i ** 2 for i in [0, 1, 3, 2, 5, 4]}