In the article Set data structure in Python, you learned about Sets. In this article, let’s look at some operations on Set data structure in Python.
1. Traverse each element of a Set
Use a for loop to traverse each element of a Set in Python.
my_set1 = {'gochocit', True, 5, 'com'}
print("my_set1:")
for x in my_set1:
print(x, end=' ')
my_set2 = {1, -7.5, 'hello', 1, 'hello'}
print("\nmy_set2:")
for x in my_set2:
print(x, end=' ')
Result
my_set1:
True com 5 gochocit
my_set2:
1 -7.5 hello
2. Set Operators
2.1. Union Operator
The union of 2 Sets will create a new Set consisting of all elements in both Sets. We can use the | operator or the union()
function to combine 2 Sets.
my_set1 = {1, 2, 3, 4, 5}
my_set2 = {4, 5, 6, 7, 8}
# Union using operator |
my_set3 = my_set1 | my_set2
print("Union using operator |:", my_set3)
# Union using union()
my_set4 = my_set1.union(my_set2)
print("Union using union():", my_set4)
Result
Union using operator |: {1, 2, 3, 4, 5, 6, 7, 8}
Union using union(): {1, 2, 3, 4, 5, 6, 7, 8}
2.2. Intersection Operator
The intersection of 2 Sets will create a new Set consisting of only elements in both Sets. We can use the & operator or the intersection()
function to intersect 2 Sets.
my_set1 = {1, 2, 3, 4, 5}
my_set2 = {4, 5, 6, 7, 8}
# Intersection using operator &
my_set3 = my_set1 & my_set2
print("Intersection using operator &:", my_set3)
# Intersection using intersection()
my_set4 = my_set1.intersection(my_set2)
print("Intersection using intersection():", my_set4)
Result
Intersection using operator &: {4, 5}
Intersection using intersection(): {4, 5}
2.3. Difference Operator
The difference between 2 Sets will create a new Set consisting of only elements in Set 1 that are not in Set 2. We can use the – operator or the difference()
function to find the difference between 2 Sets.
my_set1 = {1, 2, 3, 4, 5}
my_set2 = {4, 5, 6, 7, 8}
# Difference using operator -
my_set3 = my_set1 - my_set2
print("Difference using operator -:", my_set3)
# Difference using difference()
my_set4 = my_set1.difference(my_set2)
print("Difference using difference():", my_set4)
Result
Difference using operator -: {1, 2, 3}
Difference using difference(): {1, 2, 3}
2.4. Symmetric Difference Operator
The symmetric difference between 2 Sets will create a new Set consisting of only elements in either Set 1 or Set 2, but not in both. We can use the ^ operator or the symmetric_difference()
function to find the symmetric difference of 2 Sets.
my_set1 = {1, 2, 3, 4, 5}
my_set2 = {4, 5, 6, 7, 8}
# Symmetric Difference using operator ^
my_set3 = my_set1 ^ my_set2
print("Symmetric Difference using operator ^:", my_set3)
# Symmetric Difference using symmetric_difference()
my_set4 = my_set1.symmetric_difference(my_set2)
print("Symmetric Difference using symmetric_difference():", my_set4)
Result
Symmetric Difference using operator ^: {1, 2, 3, 6, 7, 8}
Symmetric Difference using symmetric_difference(): {1, 2, 3, 6, 7, 8}
3. Set Methods
Python supports many methods to make it easier to work with Sets. Some standard methods used with Sets are described in the table below.
Method | Description |
---|---|
add() | Adds an element to the Set |
clear() | Removes all elements from the Set |
copy() | Returns a copy of the Set |
difference() | Returns a Set containing all elements that are in Set 1 but not in Set 2 |
difference_update() | Returns Set S1 after removing elements found in Set S2 |
discard() | Remove a specific element from the Set |
intersection() | Returns a Set containing only elements that are in both Sets |
intersection_update() | Returns Set S1 keeping only elements also found in Set S2 |
isdisjoint() | Returns whether two Sets have an intersection or not |
issubset() | Returns whether another Set contains this Set or not |
issuperset() | Returns whether this Set contains another Set or not |
pop() | Removes an arbitrary element from the Set |
remove() | Removes the specified element from the Set |
symmetric_difference() | Returns a Set containing all elements that are in either Set 1 or Set 2, but not in both |
symmetric_difference_update() | Returns Set S1 with elements from Set S1 or Set S2 but not both |
union() | Returns a Set containing all elements from both Sets |
update() | Returns Set S1 with elements added from S2 |
Example using Set methods
my_set1 = {'John', 'Marry', 'Henry'}
my_set2 = {'Kane', 'Cris', 'John'}
# add()
my_set1.add('Son')
print("add my_set1:", my_set1)
# clear()
my_set1.clear()
print("clear my_set1:", my_set1)
# copy()
my_set_copy = my_set2.copy()
print("copy my_set2:", my_set_copy)
# isdisjoint()
my_set1 = {'John', 'Marry', 'Henry'}
my_set2 = {'Kane', 'Cris', 'John'}
print("my_set1 havenot a intersection with my_set2?", my_set1.isdisjoint(my_set2))
my_set1 = {'John', 'Marry', 'Henry'}
my_set2 = {'Kane', 'Cris', 'Son'}
print("my_set1 havenot a intersection with my_set2?", my_set1.isdisjoint(my_set2))
# update()
my_set1.update(my_set2)
print("update my_set1:", my_set1)
Result
add my_set1: {'Marry', 'Henry', 'Son', 'John'}
clear my_set1: set()
copy my_set2: {'Kane', 'Cris', 'John'}
my_set1 havenot a intersection with my_set2? False
my_set1 havenot a intersection with my_set2? True
update my_set1: {'Kane', 'Henry', 'John', 'Marry', 'Cris', 'Son'}