Sets in Python
How to create and manipulate sets in Python
In today’s article we will explore sets in Python. You must have learnt in your Math class about sets and if you remember a set is a collection of elements but today we will dig into the details of sets in Python which is close to, but not the same as, mathematical sets. Set is one of the four built-in data types in Python along with List, Tuple, and Dictionary, all with different qualities and usage. It is used to store collections of data and are written with curly brackets.
In Python, sets store unordered values and cannot have multiple occurrences of the same element. It makes sets highly useful to efficiently remove duplicate values from a list or tuple and to perform common math operations like unions and intersections. For example, let S be a set of even numbers less than or equal to 10.
S = set([2,4,6,8,10])
As I said, sets can neither have any order nor have repeated values.
In [ ]: set([2,4,6,8,10,8,6])Out [ ]:{2, 4, 6, 8, 10}
You can see in the above code each element can appear in the set only once. Now, Let W be a set
In [ ]: W = set(['hello','hola','bonjour'])
print(W)
Out [ ]: {'hola', 'hello', 'bonjour'}
In the example above, the order of the set W is not preserved.
Initialize a Set
In order to create a new empty set, you use set()
S = set()
Add and Delete Values from a Set
We can use update() to add new elements to a set. It adds elements from a set (passed as an argument) to the set. In the example below, I have used update() to add new elements to the empty set S.
In [ ]: S.update({1,3,5,7,9,11,13,15})
print(S)Out [ ]: {1, 3, 5, 7, 9, 11, 13, 15}
To delete an element from a set. We can use remove() and it only works if the element to be deleted is present in the set.
In [ ]: S.remove(3)
print(S)Out [ ]: {1, 5, 7, 9, 11, 13, 15}
Whereas discard() works whether the element to be deleted is present in the set or not.
In [ ]: S.discard(5)
print(S)Out [ ]: {1, 7, 9, 11, 13, 15}In [ ]: S.discard(17)
print(S)Out [ ]: {1, 7, 9, 11, 13, 15}
We can use pop() to remove a random element from the set.
In [ ]: S.pop()
print(S)Out [ ]: {7, 9, 11, 13, 15}In [ ]: S.pop()
print(S)Out [ ]: {9, 11, 13, 15}
In order to get a sorted list from a set, we use sorted() function.
In [ ]: S = {-9,1,7,5,9,-1,-5,-3,3,-7}
print(sorted(S))Out [ ]: [-9, -7, -5, -3, -1, 1, 3, 5, 7, 9]
Immutable Elements
Although a set is mutable i.e. we can add or remove elements from it, the elements of a set are immutable (cannot be changed). Immutable objects include numbers, strings and tuples. In the example below you can see that the elements of a set can be tuples.
In [ ]: T = set([(2,5),(4,7),(6,9)]
print(T)Out [ ]: {(2, 5), (6, 9), (4, 7)}
but they cannot be lists.
In [ ]: L = set([[2,5],[4,7],[6,9]])
print(L)---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-139-efadde9932a3> in <module>
----> 1 L = set([[2,5],[4,7],[6,9]])
2 print(L)TypeError: unhashable type: 'list'
Operations on sets
Python defines many operations on sets. Most operations come in two forms, as a method and as an overload of mathematical operators.
In [ ]:
# Let A be a set of even integers between 0 and 9
A = set([0,2,4,6,8])
# Let B be set of all integers between 0 and 8
B = set(range(0,9))
# C be set of odd integers between 0 and 9
C = set([1,3,5,7,9])
Element of or belongs to: To check if an element is in a set or not we use “in” and “not in” operators.
In [ ]: 9 in A, 2 not in B, 7 in COut [ ]: (False, False, True)
Subset (A⊆B ): A is a subset of B if set A is included in set B. In other words, all the elements in set A are also elements of set B. You can see in the diagram below. To investigate in Python, whether a set A is a subset of another set B or not, we use issubset() or <=.
In [ ]: A.issubset(B)Out [ ]: TrueIn [ ]: A<=BOut [ ]: True
Superset (B⊇A): B is a superset of A is another way of saying that A is a subset of B. To examine it in Python, we use issuperset() or >=.
In [ ]: B.issuperset(A)Out [ ]: TrueIn [ ]: B>=AOut [ ]: True
Union (A⋃B): A⋃B has all the elements that belong either to set A or to set B. To find union in Python, we use union() or |.
In [ ]: A.union(B)Out [ ]: {0, 1, 2, 3, 4, 5, 6, 7, 8}In [ ]: A | BOut [ ]: {0, 1, 2, 3, 4, 5, 6, 7, 8}
Intersection (A⋂B): A⋂B has all the elements that belong to both set A and set B. To determine intersection in Python, we use intersection() or &.
In [ ]: A.intersection(B)Out [ ]: {0, 2, 4, 6, 8}In [ ]: A&BOut [ ]: {0, 2, 4, 6, 8}
Difference (B-A): B-A contains all elements that are in B and not in A. To evaluate difference in Python, we use difference() or -.
In [ ]: B.difference(A)Out[ ]: {1, 3, 5, 7}In [ ]: B-AOut[ ]: {1, 3, 5, 7}
Symmetric Difference (A △ B): A △ B comprises all elements that belong to A or B but not to their intersection or contains all elements that are in one of the two sets, but not in both. We use the method symmetric_difference() or ^, to find symmetric difference in Python.
In [ ]: A.symmetric_difference(B)Out [ ]: {1, 3, 5, 7}In [ ]: A^BOut [ ]: {1, 3, 5, 7}
Computing the cartesian Product
The Cartesian product of two sets A and B, denoted A × B, is the set of all possible ordered pairs where the elements of A are first and the elements of B are second. In set-builder notation, A × B = {(a, b) : a ∈ A and b ∈ B}. The code snippet below computes the cartesian product of two sets A and B in Python.
In [ ]: # Let A be a set of even integers between 0 and 9
A = set([0,2,4,6,8])
# Let B be set of all integers between 0 and 8
B = set(range(0,9))
C = set()for x in A:
for y in B:
C.add((x,y))
COut [ ]:
{(0, 0),
(0, 1),
(0, 2),
(0, 3),
(0, 4),
(0, 5),
(0, 6),
(2, 0),
(2, 1),
(2, 2),
(2, 3),
(2, 4),
(2, 5),
(2, 6),
(4, 0),
(4, 1),
(4, 2),
(4, 3),
(4, 4),
(4, 5),
(4, 6),
(6, 0),
(6, 1),
(6, 2),
(6, 3),
(6, 4),
(6, 5),
(6, 6)}
Conclusion
In this article, we have learnt about sets in Python, how to create a set, how to add and remove elements from a set. We went over the operations that can be applied on sets. We also saw that sets themselves are mutable but their elements are immutable. Thank you for reading. I hope you found this article useful. If you have any questions, feel free to reach out in the comments below or through Twitter. Happy Pythoning!