Python - Join Sets
Python - Join Sets
Dark mode
Dark code
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R JQUERY DJANGO
Python HOME
Python Intro ADVERTISEMENT
Set Methods
set1 = {"a", "b" , "c"}
Set Exercises set2 = {1, 2, 3}
Python Dictionaries
Python If...Else set3 = set1.union(set2)
print(set3)
Try it Yourself »
Example
The update() method inserts the items in set2 into set1:
Try it Yourself »
Note: Both union() and update() will exclude any duplicate items.
ADVERTISEMENT
Example
Keep the items that exist in both set x , and set y :
ADVERTISEMENT
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.intersection_update(y)
print(x)
Try it Yourself »
The intersection() method will return a new set, that only contains the items that are present in both sets.
Example
Return a set that contains the items that exist in both set x , and set y :
z = x.intersection(y)
print(z)
Try it Yourself »
Example
Keep the items that are not present in both sets:
x.symmetric_difference_update(y)
print(x)
Try it Yourself »
The symmetric_difference() method will return a new set, that contains only the elements that are NOT present in both sets.
Example
Return a set that contains all items from both sets, except items that are present in both:
z = x.symmetric_difference(y)
print(z)
Try it Yourself »
Note: The values True and 1 are considered the same value in sets, and are treated as duplicates:
Example
True and 1 is considered the same value:
z = x.symmetric_difference(y)
Try it Yourself »
ADVERTISEMENT
ADVERTISEMENT
FORUM | ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.