Tutorials Exercises Get Certified Services Bootcamps Spaces Sign Up Log in
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
Python Get Started
Python Syntax
Python Comments
Python - Join Sets
Python Variables
Python Data Types
Python Numbers
Python Casting ❮ Previous Next ❯
Python Strings
Python Booleans
Python Operators
Python Lists Join Two Sets
Python Tuples
There are several ways to join two or more sets in Python.
Python Sets
Python Sets You can use the union() method that returns a new set containing all items from both sets, or the update() method that
Access Set Items inserts all the items from one set into another:
Add Set Items
Remove Set Items
Example Get your own Python Server
Loop Sets
Join Sets The union() method returns a new set with all items from both sets:
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:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2) COLOR PICKER
print(set1)
Try it Yourself »
Note: Both union() and update() will exclude any duplicate items.
ADVERTISEMENT
Keep ONLY the Duplicates
The intersection_update() method will keep only the items that are present in both sets.
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 :
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.intersection(y)
print(z)
Try it Yourself »
Keep All, But NOT the Duplicates
The symmetric_difference_update() method will keep only the elements that are NOT present in both sets.
Example
Keep the items that are not present in both sets:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
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:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
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:
x = {"apple", "banana", "cherry", True}
y = {"google", 1, "apple", 2}
z = x.symmetric_difference(y)
Try it Yourself »
❮ Previous Log in to track progress Next ❯
ADVERTISEMENT
ADVERTISEMENT
Spaces Upgrade Newsletter Get Certified Report Error
Top Tutorials Top References Top Examples Get Certified
HTML Tutorial HTML Reference HTML Examples HTML Certificate
CSS Tutorial CSS Reference CSS Examples CSS Certificate
JavaScript Tutorial JavaScript Reference JavaScript Examples JavaScript Certificate
How To Tutorial SQL Reference How To Examples Front End Certificate
SQL Tutorial Python Reference SQL Examples SQL Certificate
Python Tutorial W3.CSS Reference Python Examples Python Certificate
W3.CSS Tutorial Bootstrap Reference W3.CSS Examples PHP Certificate
Bootstrap Tutorial PHP Reference Bootstrap Examples jQuery Certificate
PHP Tutorial HTML Colors PHP Examples Java Certificate
Java Tutorial Java Reference Java Examples C++ Certificate
C++ Tutorial Angular Reference XML Examples C# Certificate
jQuery Tutorial jQuery Reference jQuery Examples XML Certificate
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.
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.
W3Schools is Powered by W3.CSS.