Python: Sets, Union, Intersection
This page shows you how to do {union, intersection, difference} on sets.
Difference of Sets
set1 = {1, 2, 3} set2 = {1, 4, 5} print((set2 - set1) == {4, 5}) print((set1 - set2) == {2, 3})
Union of Sets
set1 = {1, 2, 3} set2 = {1, 4, 5} print(set1.union(set2) == {1, 2, 3, 4, 5})
Intersection of Sets
set1 = {1, 2, 3} set2 = {1, 4, 5} print(set1.intersection(set2) == {1})