Learn Before
Concept
difference(set) and difference_update(set)
The difference() method returns a set containing the difference between two or more sets. The difference_update() method removes the items that exist in both sets. Usage:
colors = {"red", "yellow", "orange"} fruits = {"apple", "banana", "orange"} diff1 = colors.difference(fruits) print(diff) colors.difference_update(fruits) print(colors)
The first print statement outputs {'red', 'yellow'} as both sets contain the element 'orange'.
The second print statement outputs {'red', 'yellow'} as well, but difference_update() changes the contents of the colors set by removing 'orange'.
0
1
Updated 2021-05-31
Tags
Python Programming Language
Data Science