Learn Before
Concept
symmetric_difference() and symmetric_difference_update()
The symmetric_difference() method returns a set containing all items from both sets excluding those that are present in both. The symmetric_difference_update() method removes the items that exist in both sets and inserts the items that are not present 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', 'apple', 'banana'} as both sets contain the element 'orange'.
The second print statement outputs {'red', 'yellow', 'apple', 'banana'} as well, but symmetric_difference_update() changes the contents of the colors set by removing 'orange' and adding 'apple' and 'banana'.
0
1
Updated 2021-06-01
Tags
Python Programming Language
Data Science