symmetric difference of two set in python by R4R Team

- symmetric_difference() is a python function which is only applicable on sets.
- It find the symmetric difference of two sets.

Syntax-

set1.symmetric_difference(set2)

- It erase the common elements from the both the sets and return rest of elements.

Example

set1 is {1,2,3,4,5}
set2 is {3,4,5,6,7}

it will return {1,2,6,7} beacuse 3,4,5 is common part so erase that.


program-

set1={1,2,3,4}
set2={3,4,5,6,7,8,9}

print("Set1 is",set1)
print("Set2 is",set2)

print(set1.symmetric_difference(set2))
print(set2.symmetric_difference(set1))
print(set1.symmetric_difference(set1))
print(set2.symmetric_difference(set2))


output-

Set1 is {1, 2, 3, 4}
Set2 is {3, 4, 5, 6, 7, 8, 9}
{1, 2, 5, 6, 7, 8, 9}
{1, 2, 5, 6, 7, 8, 9}
set()
set()


-In this program, we have two sets {1,2,3,4} and {3,4,5,6,7,8,9}, here 3,4 is common items in both of sets that's by set1.symmetric_difference(set2) will print rest of elememts.
- set1.symmetric_difference(set1), i.e. symmetric difference with itself, in this case all items are common that's by it erase all and print empty set by set().




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!