To subtract two sets in Python use the difference() method. This method is called on one set and takes the second set as a parameter. Alternatively, you can also use – operator.
1. Quick Examples of Subtraction operation on Sets
Following are quick examples of how to perform Subtraction operations on sets.
# Create sets
myset1 = {12,90,43,56,"Hello"}
myset2 = {12,90,"sparkby","Hello"}
myset3 = {12,90,43,56,"Hello"}
myset4 = {67,89,90}
# Set1 - Set2 (Using difference())
print("Set1 - Set2: ",myset1.difference(myset2))
# Set1 - Set4 (Using ''-' operator)
print("Set1 - Set4: ",myset1 - myset4)
# Set1 - Set2 - Set3 (Using difference())
print("Set1 - Set2 - Set3: ",myset1.difference(myset2,myset3))
# Set2 - Set1
myset2.difference_update(myset1)
print("Set 2: ",myset2)
2. Set Subtraction using difference() method
The difference() method is used to get elements from the first set such that all these elements are not present in any of the sets.
2.1 Set difference() Syntax
Let’s see the syntax for difference().
# Syntax for difference using method
set1.difference(set2,set3,...)
2.2 Examples of difference() method
Example 1: Let’s create two sets and perform subtraction operation for both using difference() method.
# Create two sets with some elements
myset1 = {12,90,43,56,"Hello"}
print("Set 1: ",myset1)
myset2 = {12,90,"sparkby","Hello"}
print("Set 2: ",myset2)
# Set2 - Set1
print("Set2 - Set1: ",myset2.difference(myset1))
# Set1 - Set2
print("Set1 - Set2: ",myset1.difference(myset2))
# Output:
# Set 1: {43, 12, 'Hello', 56, 90}
# Set 2: {90, 12, 'sparkby', 'Hello'}
# Set2 - Set1: {'sparkby'}
# Set1 - Set2: {56, 43}
- ‘sparkby’ is only element present in set2 but not in set1.
- 56 and 43 are the elements present in set1 but not in set2.
Example 2: Let’s create four sets and perform subtraction operations using difference() method.
# Create four sets with some elements
myset1 = {12,90,43,56,"Hello"}
print("Set 1: ",myset1)
myset2 = {12,90,"sparkby","Hello"}
print("Set 2: ",myset2)
myset3 = {12,90,43,56,"Hello"}
print("Set 3: ",myset3)
myset4 = {67,89,90}
print("Set 4: ",myset4)
# Set1 - Set2
print("Set1 - Set2: ",myset1.difference(myset2))
# Set1 - Set4
print("Set1 - Set4: ",myset1.difference(myset4))
# Set4 - Set3
print("Set4 - Set3: ",myset4.difference(myset3))
# Set1 - Set3
print("Set1 - Set3: ",myset1.difference(myset3))
# Output:
# Set 1: {43, 12, 'Hello', 56, 90}
# Set 2: {90, 12, 'sparkby', 'Hello'}
# Set 3: {43, 12, 'Hello', 56, 90}
# Set 4: {89, 90, 67}
# Set1 - Set2: {56, 43}
# Set1 - Set4: {56, 43, 12, 'Hello'}
# Set4 - Set3: {89, 67}
# Set1 - Set3: set()
- 56 and 43 are the elements present in set1 but not in set2.
- 56, 43, 12, ‘Hello’ are the elements present in set1 but not in set4.
- 89, 67 are the elements present in set4 but not in set3.
- All elements are same in set1 and set3, So empty set is returned.
Example 3: Let’s create four sets and perform subtraction operations using difference() method.
# Create four sets with some elements
myset1 = {12,90,43,56,"Hello"}
myset2 = {12,90,"sparkby","Hello"}
myset3 = {12,90,43,56,"Hello"}
myset4 = {67,89,90}
# Set1 - Set2 - Set3
print("Set1 - Set2 - Set3: ",myset1.difference(myset2,myset3))
# Set4 - Set2 - Set3
print("Set4 - Set2 - Set3: ",myset4.difference(myset2,myset3))
# Output:
# Set1 - Set2 - Set3: set()
# Set4 - Set2 - Set3: {89, 67}
- We are performing subtraction on first set with second and third sets, Empty set is returned.
- 89, 67 are the elements present in set4 but not in set2 and set3.
3. Set subtraction using – operator
‘-‘ is similar to difference() method. The difference is that we will use ‘-‘ (Minus) operator instead of method.
3.1 Set ‘-‘ operator Syntax
Let’s see the syntax for ‘-‘ Minus operator.
# Syntax for difference using method
set1-set2-set3-...
3.2 Examples
Example 1: Let’s create two sets and perform subtraction operation for both using difference() method.
# Create two sets with some elements
myset1 = {12,90,43,56,"Hello"}
print("Set 1: ",myset1)
myset2 = {12,90,"sparkby","Hello"}
print("Set 2: ",myset2)
# Set2 - Set1
print("Set2 - Set1: ",myset2-myset1)
# Set1 - Set2
print("Set1 - Set2: ",myset1-myset2)
# Output:
# Set 1: {43, 12, 'Hello', 56, 90}
# Set 2: {90, 12, 'sparkby', 'Hello'}
# Set2 - Set1: {'sparkby'}
# Set1 - Set2: {56, 43}
Example 2:
Let’s perform
- Set1 – Set2 – Set3
- Set1 – Set2
# Create three sets with some elements
myset1 = {12,90,43,56,"Hello"}
print("Set 1: ",myset1)
myset2 = {12,90,"sparkby","Hello"}
print("Set 2: ",myset2)
myset3 = {12,90,43,56,"Hello"}
print("Set 3: ",myset3)
# Set1 - Set2 - Set3
print("Set1 - Set2 - Set3: ",myset1-myset2-myset3)
# Set1 - Set2
print("Set1 - Set2: ",myset1-myset2)
# Output:
# Set 1: {43, 12, 'Hello', 56, 90}
# Set 2: {90, 12, 'sparkby', 'Hello'}
# Set 3: {43, 12, 'Hello', 56, 90}
# Set1 - Set2 - Set3: set()
# Set1 - Set2: {56, 43}
4. Set subtraction using difference_update()
difference_update() is similar to difference(), But the difference is that difference() will return new set and difference_update() will store the result in the first set.
4.1 Set difference_update() Syntax
Let’s see the syntax for difference_update() method
# Syntax for difference using method
set1.difference_update(set2,set3,...)
4.2 Examples
Example 1: Let’s create two sets and subtract first set from the second set.
# Create two sets with some elements
myset1 = {12,90,43,56,"Hello"}
print("Set 1: ",myset1)
myset2 = {12,90,"sparkby","Hello"}
print("Set 2: ",myset2)
# Set2 - Set1
myset2.difference_update(myset1)
print("Set 2: ",myset2)
# Output:
# Set 1: {43, 12, 'Hello', 56, 90}
# Set 2: {90, 12, 'sparkby', 'Hello'}
# Set 2: {'sparkby'}
You can see that ‘sparkby’ is only one that exists in set2 but not in set1. And also the result is stored in myset2.
Example 2:
Let’s create two sets and subtract second set from the first set.
# Create two sets with some elements
myset1 = {12,90,43,56,"Hello"}
print("Set 1: ",myset1)
myset2 = {12,90,"sparkby","Hello"}
print("Set 2: ",myset2)
# Set1 - Set2
myset1.difference_update(myset2)
print("Set 1: ",myset1)
# Output:
# Set 1: {43, 12, 'Hello', 56, 90}
# Set 2: {90, 12, 'sparkby', 'Hello'}
# Set 1: {43, 56}
You can see that 43 and 56 exists in set1 but not in set2. And also they are stored in myset1.
5. Conclusion
In this article, we have seen how to perform set subtraction in Python using difference(),’-‘ operator and difference_update() method with examples. difference() method will return new set but the difference_update() will store the result in the first set.’-‘ operator is similar to the difference() method.
Happy Learning!!
Related Articles
- How to initialize Set in Python?
- Python set clear() method
- How to convert Python list to set?
- Python set comprehension examples
- Remove set element if exists in Python
- Find the length of the set in Python
- How to check two sets are equal in Python?
- How to convert set to string in Python?
- Join two or multiple sets in Python
- How to Append or Add Multiple Values to Set in Python?