In this tutorial, we are going to learn how to use sets in Python. how sets are created, how they’re elements are updated, and all operations that can be performed on sets.
We’ll be covering the following topics in this tutorial:
What is sets in python
A set is an unordered collection with no duplicate elements, no indexing and must be immutable (cannot be changed).
How we can use sets in Python
You can declare a variable and then is equal to, and you define the items (elements) inset in the curly bracket {}
, separated by comma, or by using the built-in set()
function. You can define multiple values and they may be of different types (integer, float, tuple, string etc.). However, a set cannot contain elements that are mutable, such as lists, sets, or dictionaries. For example
A = {1, 2, 5, 4, 7, 9}
For example, what happens when we define a set with some duplicate values. I’m going to declare the set, and you can see the 2, which was a duplicate. Which we have defined here is removed from this set.
#!/usr/bin/python A = {1, 2, 5, 4, 7, 9, 2} print(A) Output: {1, 2, 4, 5, 7, 9}
A set always has unique values, and if you define a set with duplicate values, it’s going to remove the duplicate values and only save the unique values in the set.
You can find the length of a set using this len()
method and the set’s name, which returns a set’s size.
#!/usr/bin/python A = {1, 2, 5, 4, 7, 9, 2} print(len(A)) Output: 6
Modifying a set in Python
Sets are mutable. But indexing has no meaning because it is unordered.
No element of a set can be accessed or changed by indexing or slicing. Set data type does not support it.
One element may be added with the add()
method and several items with the update()
method. Update()
can make the argument with tuples, lists, strings, or other sets. Duplicates will be avoided in all cases.
To add an element in a set. I want to add 10, for example, into my set.
#!/usr/bin/python A = {1, 2, 5, 4, 7, 9, 2} A.add(10) print(A) Output: {1, 2, 4, 5, 7, 9, 10}
You can see 10 is added to the set, but this time will only be added if it’s already not there in the set. If it’s already there in the set, nothing will happen.
Let’s try to add 10 to this set, which already has a set, and once again, you will see that nothing happens inside the set because 10 was already there.
#!/usr/bin/python A = {1, 2, 5, 4, 7, 9, 2, 10} A.add(10) print(A) Output: {1, 2, 4, 5, 7, 9, 10}
If you want to add multiple values in a set, you can use update()
methods. You can call this method, which is an update, and then inside the curly brackets {}
, you need to provide these multiple values. Let’s add some values to this set.
#!/usr/bin/python A = {1, 2, 5, 4, 7, 9, 2, 10} A.update([15, 18, 17, 14]) print(A) Output: {1, 2, 4, 5, 7, 9, 10, 14, 15, 17, 18}
You can see all these values are added to the set.
Removing elements from a set in python
You can remove one item from a set using the methods discards()
and remove()
.
You can also remove the values from the set. We can use a method called remove()
. We want to remove 18 from the set.
#!/usr/bin/python A = {1, 2, 4, 5, 7, 9, 10, 14, 15, 17, 18} A.remove(18) print(A) Output: {1, 2, 4, 5, 7, 9, 10, 14, 15, 17}
discard() method
This method works similarly to the remove()
method. For example, I want to remove this 17 from my set. I can write 17.
#!/usr/bin/python A = {1, 2, 4, 5, 7, 9, 10, 14, 15, 17} A.discard(17) print(A) Output: {1, 2, 4, 5, 7, 9, 10, 14, 15}
Difference between a discard() method and a remove() method
Whenever you use a remove()
method, you try to remove an element that is not there in the set.
#!/usr/bin/python A = {1, 2, 4, 5, 7, 9, 10, 14, 15, 17, 18} A.remove(100) print(A) Output: KeyError: 100
I will try to remove 100, which is not there in the set. It’s going to throw an exception, and it says KeyError: 100
.
Suppose you try to do the same thing using a discard()
method. Try to discard()
a hundred from the set, which is not there in the set.
#!/usr/bin/python A = {1, 2, 4, 5, 7, 9, 10, 14, 15, 17, 18} A.discard(100) print(A) Output: {1, 2, 4, 5, 7, 9, 10, 14, 15, 17, 18}
It’s not going to give me any error, and that’s the difference between this discard()
and remove()
, so remove()
throws an error. When an element is not there, but this discard()
doesn’t throw any error, it’s not going to do anything if the value is not there in the set.
pop() method
It’s going to remove any random element from your set. It doesn’t need to remove the element from the left-hand side or the right-hand side. It will remove any random element from the set. for example
#!/usr/bin/python A = {1, 2, 4, 5, 7, 9, 10, 14, 15, 17, 18} A.pop() print(A) Output: {2, 4, 5, 7, 9, 10, 14, 15, 17, 18}
clear() method
Suppose you want to clear the set. You can use a clear()
method. I can use the clear()
method to empty the set.
#!/usr/bin/python A = {1, 2, 4, 5, 7, 9, 10, 14, 15, 17, 18} A.clear() print(A) Output: set()
When I try to access the set’s values, you can see it’s an empty set with no values.
del() method
If you want to delete a set, you can use a del()
function and your set’s name.
#!/usr/bin/python A = {1, 2, 4, 5, 7, 9, 10, 14, 15, 17, 18} del A print(A) Output: NameError: name 'A' is not defined
You delete it when you try to access it. It’s going to give you an error that NameError: name 'A' is not defined
.
How to create a set using a set constructor
You can also create a set using a set constructor. Instead of these curly brackets {}
, you can write set and in the double parentheses. You need to provide the double parentheses.
#!/usr/bin/python name = set(('max', 'tom', 'den')) print(name) Output: {'tom', 'max', 'den'}
Here to create a set using the set constructor. It will also create the set called name and when I try to access the values inside the set name. You can see it has created this set of names. Also, you can convert a list into a set. Let me define the Z
variable, and then I can use a set constructor, and inside these parentheses, I can use the square brackets. Which we generally use with lists, and then you can define your list here.
#!/usr/bin/python Z = set([1, 2, 3, 4]) print(Z)
this list will be converted to a set, and you can see the result here
Output: {1, 2, 3, 4}
Mathematical sets Operation in Python
Now similar to the mathematical set operations like Union, intersection, symmetric difference, etc. You can also use these mathematical operations related to the set on the Python.
Let’s see how we can use these mathematical set operations on Python sets. Let me once again define a set. I have already one set A. which contains these values, for example
A = {2, 4, 5, 7, 9, 10, 14, 15}
and I will define a set B with some other set of values. I am going to define a set with, for example.
B = {10, 11, 12, 13, 14, 16, 18}
Union Method
Now I have two sets, and on these two sets, I want to perform some set operations, which are also used in mathematics. You can find out the union of two sets using an operator called or. when I write
#!/usr/bin/python A = {2, 4, 5, 7, 9, 10, 14, 15} B = {10, 11, 12, 13, 14, 16, 18} print(A | B) Output: {2, 4, 5, 7, 9, 10, 11, 12, 13, 14, 15, 16, 18}
It’s going to give me the Union of these two sets. Union of two sets contains all the elements in set A or inset B. you can see it’s going to give me the Union of A and B. that means this set contains all the elements in set A or set B. I can use a method called Union instead of this or operator.
I can use another way to use union methods with dot operator.
#!/usr/bin/python A = {2, 4, 5, 7, 9, 10, 14, 15} B = {10, 11, 12, 13, 14, 16, 18} print(A.union(B)) Output: {2, 4, 5, 7, 9, 10, 11, 12, 13, 14, 15, 16, 18}
Which is going to give you the same answer. You can either use print(A.union(B))
method or using print(A | B)
operator.
intersection Method
Now let’s see how we can find out the intersection between two sets. To find out the intersection, you use the ‘&’ operator.
#!/usr/bin/python A = {2, 4, 5, 7, 9, 10, 14, 15} B = {10, 11, 12, 13, 14, 16, 18} print(A & B) Output: {10, 14}
the intersection of two sets contains all the elements that are there in both the set that means set A and set B. you can see it gives me two elements inside the set and these two elements are there both in the A set and the B set. That’s why we get only two values because these two values are there in set A and set B.
you can use a method called the intersection
#!/usr/bin/python A = {2, 4, 5, 7, 9, 10, 14, 15} B = {10, 11, 12, 13, 14, 16, 18} print(A.intersection(B)) Output: {10, 14}
which is going to give me the same answer. either you can use print(A.intersection(B))
method or you can use print(A & B)
.
what is a difference between two set
A difference of two sets contains all the elements in A but not in B. you can find out the difference by the minus '-'
operator.
#!/usr/bin/python A = {2, 4, 5, 7, 9, 10, 14, 15} B = {10, 11, 12, 13, 14, 16, 18} print(A - B) Output: {2, 4, 5, 7, 9, 15}
When you write A minus B, you will be able to differentiate between these two sets. This result will contain all the elements that are in A but not in B. you can also use B minus A, and then is going to give you other results, because this time it’s going to provide you with a set which contains all the elements that are there in B and not in A. difference between set A minus B is different from B minus A.
Also, you can use a different method.
#!/usr/bin/python A = {2, 4, 5, 7, 9, 10, 14, 15} B = {10, 11, 12, 13, 14, 16, 18} print(A.difference(B)) Output: {2, 4, 5, 7, 9, 15}
it’s going to give you the same kind of answer.
Symmetric Difference between two Sets
Symmetric difference between two sets contains all the elements in set A but not in set B, or they are inset B but not in set A. you can find out the symmetric difference using the '^'
symbol and then B.
#!/usr/bin/python A = {2, 4, 5, 7, 9, 10, 14, 15} B = {10, 11, 12, 13, 14, 16, 18} print(A ^ B) Output: {2, 4, 5, 7, 9, 11, 12, 13, 15, 16, 18}