Write a Python Program to Find Elements in a Given Set That Are Not in Another Set

Write a python program to find the elements in a given set that are not in another set is provided on this page.

Write A Python Program To Find The Elements In A Given Set That Are Not In Another Set

Sets in python are stored using curly braces {}.

Finding the elements in a given set that are not in another set is equivalent to finding the difference in terms of elements and there is one function which performs this exact job, its called difference().

Also we can simply use - operator to get difference.

Both ways are demonstrated with python program below.


set_1 = {10, 20, 30, 40, 50}
set_2 = {40, 50, 60, 70, 80}

print("Set in Original State : ")
print(set_1)
print(set_2)

print("Difference of set_1 and set_2 using difference() : ")
print(set_1.difference(set_2))

print("Difference of set_2 and set_1 using difference() : ")
print(set_2.difference(set_1))

print("Difference of set_1 and set_2 using - operator : ")
print(set_1 - set_2)

print("Difference of set_2 and set_1 using - operator : ")
print(set_2 - set_1)

Related To Python

Comments