Overview
Python Set Methods consist of various Set Methods like union, intersection, difference, etc. Exploring the set methods is important for efficient data manipulation in Python. Knowing when and how to use these methods can significantly enhance your coding efficiency, particularly in tasks involving unique data collections.
What’s Next!
Let’s move toward the Python Set Methods. Here we will walk you through some common Set Methods, important for data filtering, membership testing, or handling distinct elements. Let’s jump into the code and master the Python set methods to write clean and more effective code.
Table of Contents
Python Set Methods
1. Union
set1 = {'A', 'B', 'C', 'D'}
set2 = {'C', 'D', 'E', 'F'}
union_set = set1.union(set2)
print('Union of both set1 and set2:',union_set)
2. Update
set1 = {'A', 'B', 'C', 'D'}
set2 = {'C', 'D', 'E', 'F'}
set1.update(set2)
print('After updating set1:',set1)
3. Intersection
set1 = {'A', 'B', 'C', 'D'}
set2 = {'C', 'D', 'E', 'F'}
intersection_set = set1.intersection(set2)
print('Intersection of both set1 and set2:',intersection_set)
4. Intersection_update
set1 = {'A', 'B', 'C', 'D'}
set2 = {'C', 'D', 'E', 'F'}
intersection_set = set1.intersection_update(set2)
print('After intersection_update of set1 with set2:',set1)
5. Symmetric_difference
set1 = {'A', 'B', 'C', 'D'}
set2 = {'C', 'D', 'E', 'F'}
symmetric_difference_set = set1.symmetric_difference(set2)
print('Symmetric difference of both set1 and set2:',symmetric_difference_set)
6. Symmetric_difference_update
set1 = {'A', 'B', 'C', 'D'}
set2 = {'C', 'D', 'E', 'F'}
symmetric_difference_set = set1.symmetric_difference_update(set2)
print('After symmetric_difference_update of set1 with set2:',set1)
7. difference
set1 = {'A', 'B', 'C', 'D'}
set2 = {'C', 'D', 'E', 'F'}
difference_set = set1.difference(set2)
print('Difference of both set1 and set2:',difference_set)
8. difference_update
It updates the original set by removing the common elements that are also present in the other set.
set1 = {'A', 'B', 'C', 'D'}
set2 = {'C', 'D', 'E', 'F'}
difference_set = set1.difference_update(set2)
print('After difference_update of set1 with set2:',set1)
10. issuperset
set1 = {'A', 'B', 'C', 'D'}
set2 = {'C', 'D'}
is_superset = set1.issuperset(set2)
print('Is set1 a superset of set2:',is_superset)
11. issubset
set1 = {'A', 'B', 'C', 'D'}
set2 = {'C', 'D'}
is_subset = set1.issubset(set2)
print('Is set1 a subset of set2:',is_subset)
12. add
set1 = {'A', 'B', 'C', 'D'}
set1.add('E')
print('After adding single element:',set1)
13. remove
set1 = {'A', 'B', 'C', 'D'}
# element present in the set
set1.remove('B')
# element is not present in the set
set1.remove('F')
print('After removing the element present in the set:',set1)
print('After removing the element not present in the set:',set1)
Output - KeyError: 'F', arises because you're trying to remove an element ('F') from the set set1 using the .remove() method, but this element doesn't exist within the set.
14. discard
It removes an element from the set but does not raise an error if the element is not present in the set.
set1 = {'A', 'B', 'C', 'D'}
set1.discard('E')
print('After discarding element:',set1)
15. del
set1 = {'A', 'B', 'C', 'D'}
del set1
print('After deleting the set:',set1)
Output - NameError: name 'set1' is not defined.
16. clear
It deletes all elements from the set and returns the blank set.
set1 = {'A', 'B', 'C', 'D'}
set1.clear()
print('After clearing the set:',set1)
17. in
It checks if the particular element is present in the set.
set1 = {'A', 'B', 'C', 'D'}
if 'A' in set1:
print('Element is present in the set.')
else:
print('Element is not present in the set.')
18. pop
set1 = {'A', 'B', 'C', 'D'}
set1.pop()
print('After popping the element:',set1)