Python Set Methods
Python Set Methods
Python sets are a collection of distinct and unordered elements. The manipulation of sets and different set operations can be performed using Set built-in methods. You can call these methods on set objects.
The following sections cover each of the methods of Set class objects with description, link to the dedicated tutorial for the Set method, and an example program with output.
1. Set add() method
Python Set add() method adds given element to the set.
Tutorial - Python Set add()
In the following program, we shall add an element 'g'
to the set set_1.
Python Program
set_1 = {'a', 'b', 'c'}
set_1.add('g')
print(set_1)
Output
{'g', 'c', 'a', 'b'}
2. Set clear() method
Python Set clear() method clears all the elements in the set.
Tutorial - Python Set clear()
In the following program, we shall take a Python set set_1 with some initial elements in it. Then, we shall clear the set set_1 use clear() method.
Python Program
set_1 = {'a', 'b', 'c'}
set_1.clear()
print(set_1)
Output
set()
3. Set copy() method
Python Set copy() method returns a shallow copy of the original set.
Tutorial - Python Set copy()
In the following program, we shall copy the contents of a set set_1 into another variable set_2.
Python Program
set_1 = {32, 56, 19}
set_2 = set_1.copy()
print(set_2)
Output
{32, 56, 19}
4. Set difference() method
Python Set difference() method returns the set of elements that are present in this set and not present in the other set.
Tutorial - Python Set difference()
In the following program, we shall find the difference of the sets, set_1 - set_2
using difference() method.
Python Program
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'c', 'd', 'e', 'f'}
set_3 = set_1.difference(set_2)
print(set_3)
Output
{'a', 'b'}
5. Set difference_update() method
Python Set difference_update() method updates the set with the difference of the other set from the set.
Tutorial - Python Set difference_update()
In the following program, we find the difference of the sets set_1 and set_2, and update the original set set_1 with the resulting set.
Python Program
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'c', 'd', 'e', 'f'}
set_1.difference_update(set_2)
print(set_1)
Output
{'a', 'b'}
6. Set discard() method
Python Set discard() method removes a specified element from the set.
Tutorial - Python Set discard()
In the following program, we take a set of elements in set_1 and remove the element 'c'
from the set.
Python Program
set_1 = {'a', 'b', 'c', 'd'}
set_1.discard('c')
print(set_1)
Output
{'a', 'd', 'b'}
7. Set intersection() method
Python set intersection() method returns the intersection of the two sets.
Tutorial - Python set intersection()
In this example, we shall take two sets in set_1 and set_2 and find their intersection.
Python Program
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'a', 'b', 'd', 'm'}
set_output = set_1.intersection(set_2)
print(set_output)
Output
{'a', 'd', 'b'}
8. Set intersection_update() method
Python Set intersection_update() method updates the set with the result of intersection operation between the set and a given iterable.
Tutorial - Python Set intersection_update()
In this example, we shall take two sets in set_1 and set_2. We shall update the set set_1 with the intersection of set_1 and set_2.
Python Program
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'a', 'k', 'd', 'm'}
print(f"set_1 before update : {set_1}")
set_1.intersection_update(set_2)
print(f"set_1 after update : {set_1}")
Output
set_1 before update : {'c', 'a', 'd', 'b'}
set_1 after update : {'a', 'd'}
9. Set isdisjoint() method
Python Set isdisjoint() method is used to check if the two sets are disjoint, i.e., if they have an intersection or not.
Tutorial - Python Set isdisjoint()
In this example, we shall take two sets in set_1 and set_2. We have to check if these two sets are disjoint.
Python Program
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'k', 'm'}
if set_1.isdisjoint(set_2):
print("set_1 and set_2 are DISJOINT.")
else:
print("set_1 and set_2 are NOT DISJOINT.")
Output
set_1 and set_2 are DISJOINT.
10. Set issubset() method
Python Set issubset() method is used to check if the set is a subset of given set.
Tutorial - Python Set issubset()
In the following program, we take two sets in set_1 and set_2. We shall check if the set set_1 is a subset of the set set_2.
Python Program
set_1 = {'a', 'c'}
set_2 = {'a', 'b', 'c', 'd'}
if set_1.issubset(set_2):
print("set_1 is a SUBSET of set_2.")
else:
print("set_1 is NOT a SUBSET of set_2.")
Output
set_1 is a SUBSET of set_2.
11. Set issuperset() method
Python Set issuperset() method is used to check if the set is a superset of another given set.
Tutorial - Python Set issuperset()
In the following program, we take two sets in set_1 and set_2. We shall check if the set set_1 is a superset of the set set_2.
Python Program
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'a', 'c'}
if set_1.issuperset(set_2):
print("set_1 is SUPERSET of set_2.")
else:
print("set_1 is NOT SUPERSET of set_2.")
Output
set_1 is SUPERSET of set_2.
12. Set pop() method
Python Set pop() method removes a random item from the set and returns the removed item.
Tutorial - Python Set pop()
In the following program, we take a set of strings in set_1 and remove a random item from the set using pop() method.
Python Program
set_1 = {'apple', 'banana', 'cherry'}
removed_item = set_1.pop()
print(f"Removed item : {removed_item}")
print(f"Updated set : {set_1}")
Output
Removed item : cherry
Updated set : {'apple', 'banana'}
13. Set remove() method
Python Set remove() method removes the specified element from the set.
Tutorial - Python Set remove()
In the following program, we take a set set_1 with three items and remove the element 'b' from the set.
Python Program
set_1 = {'a', 'b', 'c'}
set_1.remove('b')
print(set_1)
Output
{'a', 'c'}
14. Set symmetric_difference() method
Python Set symmetric_difference() method returns the difference of union of the two sets and intersection of the sets.
Tutorial - Python Set symmetric_difference()
In the following program, we shall take two sets in set_1 and set_2. We shall find the symmetric difference of these two sets.
Python Program
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'a', 'b', 'd', 'm'}
symm_diff_output = set_1.symmetric_difference(set_2)
print(symm_diff_output)
Output
{'c', 'm'}
15. Set symmetric_difference_update() method
Python Set symmetric_difference_update() method updates the original set with the symmetric difference of the set and the given iterable.
Tutorial - Python Set symmetric_difference_update()
In the following program, we shall take two sets in set_1 and set_2. We shall update the set_1 with the symmetric difference of the two sets.
Python Program
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'a', 'b', 'd', 'm'}
print(f"set_1 : {set_1}")
set_1.symmetric_difference_update(set_2)
print(f"set_1 after update : {set_1}")
Output
set_1 : {'d', 'b', 'c', 'a'}
set_1 after update : {'m', 'c'}
16. Set union() method
Python Set union() method returns the union of the two sets.
Tutorial - Python Set union()
In the following program, we take two sets in set_1 and set_2 and find their union.
Python Program
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'a', 'b', 'd', 'm'}
union_output = set_1.union(set_2)
print(union_output)
Output
{'a', 'c', 'd', 'm', 'b'}
17. Set update() method
Python Set update() method updates the set with the items from given iterable. The iterable could be another set, a list, a tuple, a string, etc.
Tutorial - Python Set update()
In the following program, we shall take two sets in set_1 and set_2. We shall update the set set_1 with the items from the set set_2, using update() method.
Python Program
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'a', 'b', 'd', 'm'}
print(f"set_1 before update : {set_1}")
set_1.update(set_2)
print(f"set_1 after update : {set_1}")
Output
set_1 before update : {'d', 'c', 'b', 'a'}
set_1 after update : {'a', 'c', 'b', 'd', 'm'}