Introduction to Python Sets
A set in a mathematical expression means a group of data and elements that are arranged in a specific order and represented by enclosing the elements in the curly parenthesis. Python being an object oriented programming language used for the web based application development process, where a set can have only distinctive values without any duplication of the items. The syntax contains a set name that is assigned to the set of data or elements enclose between the curly parenthesis and separated by a delimiter, that is comma.
Syntax:
As in general python, the syntax is generally easy. The syntax for python set is as follows:
firstset = {"Johnny", "Nilanjan", "Rupa"}
print(firstset)
Here, the first set is the variable name in which the set is stored. The curly braces {} represents set and since we are adding string values so double/single inverted commas are required. The values in the set are separated by commas. Now, since we have seen the syntax of the set with an example in Python. Let us now discuss the different methods used in Python sets.
Different methods in Python sets
Let us go through the different methods present as built-in Python for Sets.
1. add(): As the name suggests it used to add a new element in the set. It means you are increasing the number of elements in the set by one. Here one very important knowledge about set that needs to be kept in mind is that the element only gets added if it is not already present in the set assets do not take duplicate elements. The add method also does not return any value. Let us do an example.
Code:
firstset = {"Johnny", "Nilanjan", "Rupa"}
firstset.add("Sepoy")
print("The new word is",firstset)
#to check duplicate property of Set
firstset.add("Sepoy")
print("The new word is",firstset)
4.8 (7,854 ratings)
View Course
Now the below screenshot is the output of the code when it is run on Jupyter Notebook.
If you see the output the first time, when add() function is used it adds the element and the size of the set is increased by one as shown when we execute the first print statement but the second time when we use add() method to add the very same element (sepoy) like the first time, when executing the print statement we see the same elements getting displayed with no increase in the size of the set which means that set does not any take duplicate values.
2. clear(): As the name suggest it removes all the elements from the set. It neither takes any parameter nor does it return any value. We simply just have to call the clear method and execute it. Let us look at an example:
Code:
firstset = {"Johnny", "Nilanjan", "Rupa"}
print("Before clear",firstset)
firstset.clear()
print("After clear",firstset)
Let us look at the output after executing the same code in the jupyter Notebook.
So, the above screenshot shows that before we had executed the clear method the list was printed with elements and then when we executed the clear() method all the elements were removed and we are left with an empty set.
3. copy(): This method is used to create a shallow copy of a set. The term shallow copy means that if you add new elements in the set or remove elements from the set, the original set does not change. It is the basic advantage of using the copy function. We will see an example to understand the shallow copy concept.
Code:
originalset = {"Johnny", "Nilanjan", "Rupa"}
copiedset = originalset.copy()
print("originalset:: ",originalset)
print("copiedset:: ",copiedset)
# modify the copiedset to check shallow copy feature
copiedset.add("Rocky")
print("originalset:: ",originalset)
print("copiedset:: ",copiedset)
Now let us check the output in Jupyter Notebook.
As you can see that when we used to add function to add a new element in the copied set, the copied set got modified but the original set still remained the same.
4. difference(): This is a very important function inset. This function returns a set which is the difference between two sets. Keep in mind that here difference does not mean subtraction because here it is the difference between the number of elements in two sets and not the values of elements. Here for example set A1 – set A2 means it returns a set with elements present in A1 but not in A2 and vice versa in case of set A2 – set A1 (present in A2 but not in A1). The same will be explained below with the help of an example.
Code:
A1= {24, 35, 34, 45}
A2= {24, 56, 35, 46}
print(A1.difference(A2))
print(A2.difference(A1))
Now let us look at the output provided in the screenshot below.
Now, in the above screenshot if you look carefully there is a difference between the first and second results. In the first result, it is shown the elements which are in A but not in B whereas in the second result it is shown elements present in B but not in A.
5. intersection(): It is very different from the previous method built-in set. In this case, only the elements which are common in both the sets or in multiple sets (in case of more than two sets) are returned in the form of a set. Now let us go through an example.
Code:
A1= {24, 35, 34, 45}
A2= {24, 56, 35, 46}
A3= {24, 35, 47, 56}
print(A1.intersection(A2, A3))
As you can see that the three sets only had two elements in common which are 24 and 35. Hence on executing the code it returned a set containing only 24 and 35.
6. union(): It is a function that returns a set with all elements of the original set and also the specified sets. Since it returns a set so all the items will have only one appearance. If two sets contain the same value then the item will appear only once.
Code:
A1= {24, 35, 34, 45}
A2= {24, 56, 35, 46}
A3= {24, 35, 47, 56}
print(A1.union(A2, A3))
In the above screenshot, you can see the output of the code on execution. If you look closely you will find all values from A1 and all unique values from the other two sets.
7. issubset(): This function returns Boolean values that are true or false. If all the elements of one set are present in another set then it returns true otherwise false. We will see an example of the same to understand better.
Code:
A1 ={3,6,8}
A2 ={45,87,3,67,6,8}
print(A1.issubset(A2))
print(A2.issubset(A1))
If you see the above output screenshot you can see that A2 has all the elements of A1 but A1 does not have all the elements of A2. Hence A1 is a subset of A2.
8. issuperset(): This function returns Boolean values that are true or false. If a set contains all elements of another set then that set can be called a superset of the other set and the value returned by the function is true otherwise false. We will see an example of the same to understand better.
Code:
A1 = {3, 6, 8}
A2 = {45, 87, 3, 67, 6, 8}
print(A1.issuperset(A2))
print(A2.issuperset(A1))
As you can see from the output screenshot that the second set A2 contains all elements of set A1. Hence it is a superset of A1. The same is not true for A1 with respect to A2, hence it returns false.
9. remove(): This function is used to remove elements from the set. The elements to be removed are passed as arguments. The function removes the element if it is present in the set otherwise it returns an error. We will execute an example to check this.
Code:
firstset = {"Johnny", "Nilanjan", "Rupa"}
firstset.remove("Nilanjan")
print(firstset)
# to check error
firstset.remove("Rocky")
If you see the screenshot above when the code is executed it removes the element “Nilanjan” as it was present in the set but when we try to remove ”Rocky” it gives us an error as “Rocky” is not present in the set.
10. discard(): This built-in method is also used to remove elements from the set but it is different from the remove method which we discussed earlier. If the element is present in the set it removes the element but if it is present it returns no error and normally just prints the set. We will see an example of this
Code:
firstset = {"Johnny", "Nilanjan", "Rupa"}
firstset.discard("Nilanjan")
print(firstset)
firstset.discard("Rocky")
print(firstset)
If we see the above screenshot we can see that even though “Rocky” is not present in the set we see no error being displayed unlike in the case of remove method where an error was displayed.
Conclusion
We have discussed in this article the concept of sets in python and the different functions that can be used or applied in sets. Sets, as discussed, are important in python and the built-in methods are used to manipulate the sets and also to perform operations with sets.
Recommended Articles
This is a guide to the Python Sets. Here we discuss the introduction of the Python Sets, Different methods in Python sets along with Syntax. You can also go through our other suggested articles to learn more–