In this Python tutorial, we’ll learn about multiple Python set methods. Step by step explanation with proper practical code examples will be provided in order for better understanding.
Introduction: Python Set Methods
Python set is an unordered collection of items. Duplicates items should not be used and if specified, then only the first occurence of that item will be used. We’ll programmatically understand this concept as well in this article.
Python set has methods that we’ll uncover in the below sections. They can be used for intersection, union, difference, superset, subset and many more. Let’s now understand the methods of Python set but first let’s see what happen to the duplicate items in Python set.
Duplicate Items In Python Set
a = {'Zeeshan','Yasir','Usman','Zeeshan','Saqib','Usman'} print( a )
Output
{'Usman', 'Yasir', 'Saqib', 'Zeeshan'}
We can see that the duplicate items are removed when this set is used.
Implementing Python Set Methods (Easy Examples)
We’ll go through some of the most used/important methods of Python set. These are listed below:
- Intersection
- Union
- Difference
- Super set
- Sub set
Intersection
This method is used to return a set having items that are present in both sets. For demonstration, we’ll create two Python sets. See below:
a = {'Zeeshan','Yasir','Usman'} b = {'Yasir','Furqan','Zeeshan','Saqib'} res = a.intersection(b) print( res )
Output
{'Yasir', 'Zeeshan'}
We can also use the intersection method of set b. See below:
res = b.intersection(a) print( res )
Output
{'Zeeshan', 'Yasir'}
The position is changed but output remains the same. These two items are present in both sets so they are returned in set by the intersection method.
Union
The union method is used to return a set that contains all the items present in both sets (no duplicate of items). See below:
a = {'Yasir','Zeeshan','Usman'} b = {'Furqan','Zeeshan','Yasir','Saqib'} result = a.union(b) print( result )
Output
{'Saqib', 'Furqan', 'Usman', 'Yasir', 'Zeeshan'}
Difference
This method is used to return a set of items that are present in the set A but not present in set B. See below code:
a = {'Zeeshan','Saqib','Hasan'} b = {'Furqan','Zeeshan','Yasir','Saqib'} result = a.difference(b) print( result )
Output
{'Hasan'}
We can see that the item ‘Hasan’ is present in set A only so its returned. Let’s now use the difference method of set B and pass A as an argument. See below example:
result = b.difference(a) print( result )
Output
{'Yasir', 'Furqan'}
In this case, the difference method has returned a set which include items that are present in set B but not in set A.
Super set
This method returns a Boolean(True/False) value. It’ll return true if all the items in the specified set is present in the original set. For instance A.issuperset(B). In this case, A is the original set while B is the specified set. See below example:
a = {'Zeeshan','Furqan','Hasan'} b = {'Zeeshan','Hasan'} result = a.issuperset(b) print( result )
Output
True
True is returned as all the items of specified set B are present in the original set A. Let’s now make set B original and set A a specified set. See below:
result = b.issuperset(a) print( result )
Output
False
False is returned as all the items of specified set A are not present in the original set B.
Sub set
This method also return a Boolean(True/False) value. It is actually the opposite of super set. True is returned if all the items of original set is also present in the specified set. See below code:
a = {'Zeeshan','Furqan',} b = {'Zeeshan','Hasan','Furqan','Usman'} res = a.issubset(b) print( res )
Output
True
All the items of original set (A) is present in the specified set (B) so true is returned. Let’s specify the set B to original and set A to specified set. See below:
res = b.issubset(a) print(res)
Output
False
False is returned as all the items of set B is not present in set A.
These were some of the important Python set methods. Your assignment is to explore other methods of Python set and share your experience with us. Click here to explore more methods of Python set.
So this is how we can easily use Python set methods. Don’t hesitate to share it with other Python programmers.
Also, feel free to ask if you still have any queries related to Python set methods. We’ll be glad to solve all your queries.
Click here if you want to learn how to properly convert Python set to list using 4 different ways.
Conclusion
In conclusion, hope you now have an in-depth knowledge of how to use Python set methods. We’d be glad to have your valuable feedback on this post.
Do feel free to visit our other posts on Python programming to learn about other Python concepts in detail. Thank you for reading this article.