Python Set Methods 101: A Beginner Guide to Python Set

Python-Set-Methods
Credit: Canva

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

Note:
1. Use of _update – It updates the original set and returns the existing or original set.
2. No use of _update – It returns the new set.

Python Set Methods

1. Union

It is the collection of unique elements of both sets.
				
					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

It adds the unique elements from another set to the original set.
				
					set1 = {'A', 'B', 'C', 'D'}
set2 = {'C', 'D', 'E', 'F'}
set1.update(set2)
print('After updating set1:',set1)
				
			

3. Intersection

It is the collection of common elements of the both sets.
				
					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

It updates the original set from the common elements of both the sets i.e., original and another set.
				
					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

It is the opposite of intersection i.e., collection of uncommon or unique elements of both sets.
				
					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

It updates the original set with the unique elements of another set i.e., elements are in either of the sets but not in both.
				
					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

It is the collection of unique elements of the the original set, i.e., elements present in the first set but not in any of the other sets.
				
					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

It returns True, if the original set contains all the elements of another set, otherwise False.
				
					set1 = {'A', 'B', 'C', 'D'}
set2 = {'C', 'D'}
is_superset = set1.issuperset(set2)
print('Is set1 a superset of set2:',is_superset)
				
			

11. issubset

It returns True, if the another set contains all the elements of original set, otherwise False.
				
					set1 = {'A', 'B', 'C', 'D'}
set2 = {'C', 'D'}
is_subset = set1.issubset(set2)
print('Is set1 a subset of set2:',is_subset)
				
			

12. add

It adds a single element to the set.
				
					set1 = {'A', 'B', 'C', 'D'}
set1.add('E')
print('After adding single element:',set1)
				
			

13. remove

It removes an element from the set but raises an error if element is not present in the set.
				
					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

It deletes the whole set.
				
					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

It removes the last element from the set but set is unordered therefore, any random element will be removed.
				
					set1 = {'A', 'B', 'C', 'D'}
set1.pop()
print('After popping the element:',set1)
				
			

Recent Articles