A quick overview of python set.
Set is a data structure in Python, which is unordered and unindexed. The set has no duplicate elements. Every set element is
unique and immutable but the set itself is mutable. That means we can’t change the value of the set element but we can add or remove elements from the set. Sets are written within curly brackets.
Topics covered in this story
Creating Python Set
Accessing Set Elements
Set elements are unordered, so it can’t be accessed. But we can loop through the set element.
Add Items to Set
We can use add() and update() method to add elements to set.
add() used to add one element to set.
update() method used to add multiple elements to set.
Note: Both add() and update() methods will modify the original set itself. It won’t return anything. The return type is None.
Removing an Element from the Set
remove() will remove the specified element from the set. If the element is not in set means, it will raise an error.
discard() will also remove the specified element from the set. If the element is not in set means, it won’t raise an error.
Since the set is unordered, pop() will remove any element from the set.
clear() will empty the set.
del keyword will delete the set itself.
Note: remove(),discard(),clear() methods will modify the original set itself.It won’t return anything.Return Type is None.
pop() method will return the removed element.Return value is removed element.
copy method will return a copy of the set.
Mathematical Set Operations
- union(): Return new set containing union of both set.Union means return all elements from both sets.Union is performed by |operator or by union() method
- intersection(): Return new set containing intersection of both set.Intersection means returns only common element between both set.Intersection is performed by & operator or by intersection() method.
3. difference(): Returns a new set containing the difference between both sets. A-B means returns elements from set A which is not present in set B. The difference can be performed by
-operator or using the difference() method.
4. difference_update(): Removes the element from the set which is also present in the other set.It is performed by using
-= operator or by using the difference_update() method. The return type is None. It will modify the original set itself.
5. symmetric_difference(): Returns a new set containing elements from both sets excluding common elements in both sets.It is performed by using
^ operator or by using the symmetric_difference() method.
6. symmetric_difference_update(): Updates the set by keeping elements in both sets excluding common elements in both sets. It is performed by using ^= operator or by using the symmetric_difference_update() method. The return type is None. It will modify the original set itself.
7.intersection_update(): Updates the set with elements that are common to both sets.It is performed by
&= operator or by using intersection_update() method.Return Type is None. It will update the original set itself.
8. isdisjoint(): Returns
True if both sets have no common elements.Returns
False if both sets have common elements.
9. issubset(): Returns
True if all elements in this set are present in the specified set or otherwise Returns
10. issuperset(): Returns True if all elements in the specified set are present in the original set or otherwise Returns False.
We can check whether an item is present in the set or not by using
Built-in Functions -Set
- len(): Returns the number of elements in the set
- min(): Returns the smallest element in the set
- max(): Returns the largest element in the set
- sum(): Returns the sum of all elements in the set.
5. sorted(): Returns a new
list containing sorted elements. Since the set is unordered, the return type is List.
6. all(): Returns
True if all elements in the set are
True or if the set is empty.
7. any(): Returns True if any element in the set is
True.If the set is empty returns
- Set methods which will modify the original set.
Return Type is None.
- Return Type Boolean
- Return Type List
- Return type removed element
- Returns new set and won’t modify the original set.
My other blogs on Python data structures
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee