The Python set difference() is used to get the elements that are only present in the set object and not present in the other set. In this article, we will discuss how to perform the difference operation on the given sets in Python.
As we know that Python Set is an unordered data structure that will not allow duplicates. If you want to return only the elements that exist in the first set but not in the remaining sets, then you have to use difference() method or using – (minus) operator.
1. Quick Examples of Difference Python Set
Below are quick examples of getting differences in the set.
# perform difference on id and cost
print("id - cost - ",id.difference(cost))
# perform difference on id, cost and quantity
print("id - cost - quantity - ",id.difference(cost,quantity))
# perform difference on id and cost
print("id - cost - ",id-cost)
# perform difference on id, cost and quantity
print("id - cost - quantity - ",id-cost-quantity)
2. Python set difference()
difference() in Python Set will return only elements from the set such that these elements will not be present in the other sets. It will take single/multiple sets as parameters and it returns the elements in a new set.
2.1 Syntax of set difference()
The following is the syntax of the difference() function.
# syntax of difference
set1.difference(set2,set3,.....)
Let’s see some examples to understand this concept better.
2.2 set difference() Example
In this example, we will create three sets with integer elements and by using Python difference(), will merge them into a single set object. To create a set in Python use {}.
# Create three sets
id={1,2,3,4,5}
cost={10,20,34,56,1}
quantity={45,6,1,2,3}
#Actual elements in the sets.
print("Set id :",id)
print("Set cost :",cost)
print("Set quantity :",quantity)
# Output
#Set id : {1, 2, 3, 4, 5}
#Set cost : {1, 34, 10, 20, 56}
#Set quantity : {1, 2, 3, 6, 45}
Let’s perform the difference operation on these set objects.
# perform difference on id and cost
print("id - cost - ",id.difference(cost))
# perform difference on id and quantity
print("id - quantity - ",id.difference(quantity))
# perform difference on id, cost and quantity
print("id - cost - quantity - ",id.difference(cost,quantity))
# Output
# id - cost - {2, 3, 4, 5}
# id - quantity - {4, 5}
# id - cost - quantity - {4, 5}
Explanation:
- We returned elements from id set that are not present in cost set.
- We returned elements from id set that are not present in quantity set.
- Finally we are also returning the elements from id set that are not present in cost and quantity sets.
4. set difference() with String Values
In this example, we will create three Python set objects with string elements and by using difference() return only elements that exists in first set.
# Create four sets
village1={"fields","plants","electricity","houses"}
village2={"fields","plants","electricity","houses","buildings"}
village3={"None"}
# Actual elements in the sets.
print("Set-1",village1)
print("Set-2",village2)
print("Set-3",village3)
# Output
# Set-1 {'electricity', 'fields', 'houses', 'plants'}
# Set-2 {'electricity', 'fields', 'buildings', 'houses', 'plants'}
# Set-3 {'None'}
Let’s perform set difference on this set of strings.
# perform difference on village1 and village2
print("village1 - village2 - ",village1.difference(village2))
# perform difference on village2 and village3
print("village2 - village3 - ",village2.difference(village3))
# Output
# village1 - village2 - set()
# village2 - village3 - {'fields', 'electricity', 'plants', 'buildings', 'houses'}
Explanation:
- We returned only elements from village1 but not in village2.
- We returned only elements from village2 but not in village3.
5. Python Set Difference using – operator
We can also perform set differences using the – set operator in Python. This we can call as “minus” operator.
Syntax:
# Syntax
set1-set2-set3-.....
Let’s see some examples to understand this concept better.
5.1 Using – with Examples
In this example, we will create three sets with integer elements and return the elements that exists only in first set.
# Create three sets
id={1,2,3,4,5}
cost={10,20,34,56,1}
quantity={45,6,1,2,3}
# Actual elements in the sets.
print("Set-1",id)
print("Set-2",cost)
print("Set-3",quantity)
# Output
#Set-1 {1, 2, 3, 4, 5}
#Set-2 {1, 34, 10, 20, 56}
#Set-3 {1, 2, 3, 6, 45}
Output:
# perform difference on id and cost
print("id - cost - ",id-cost)
# perform difference on id and quantity
print("id - quantity - ",id-quantity)
# perform difference on id, cost and quantity
print("id - cost - quantity - ",id-cost-quantity)
# Output
# id - cost - {2, 3, 4, 5}
# id - quantity - {4, 5}
# id - cost - quantity - {4, 5}
Explanation:
- We returned elements from id set that are not present in cost set.
- We returned elements from id set that are not present in quantity set.
- Finally we are also returning the elements from id set that are not present in cost and quantity sets.
5.2 Using – with set of Strings
In this example, we will create three sets with string elements and return the elements that exists only in first set.
# Create four sets
village1={"fields","plants","electricity","houses"}
village2={"fields","plants","electricity","houses","buildings"}
village3={"None"}
# Actual elements in the sets.
print("Set-1",village1)
print("Set-2",village2)
print("Set-3",village3)
# Output
#Set-1 {'electricity', 'fields', 'houses', 'plants'}
#Set-2 {'electricity', 'fields', 'buildings', 'houses', 'plants'}
#Set-4 {'None'}
Output:
# perform difference on village1 and village2
print("village1 - village2 - ",village1-village2)
# perform difference on village2 and village3
print("village2 - village3 - ",village2-village3)
# Output
# village1 - village2 - set()
# village2 - village3 - {'fields', 'electricity', 'plants', 'buildings', 'houses'}
Explanation:
- We returned only elements from village1 but not in village2.
- We returned only elements from village2 but not in village3.
6. Differences between difference() and – operator
Though both difference() and – operator is used to return elements that exist only in the first set in Python, they both have some differences. Below I have covered some differences.
DIFFERENCE() | – OPERATOR |
---|---|
difference() is a method | – is an operator |
difference() method explicitly fix the priority of the sets | – operator will have specific operator precedence of the sets |
difference() is used only with sets | – operator can be used with other data structures |
For more examples of sets refer to Python set Methods and Python set Operators.
7. Conclusion
In this article, you have learned how to return the different elements that exist only in the first Python set but not in other sets using difference() function and – operator. You can use difference() and – operator to apply differences with two or multiple sets.