There are several ways to join two or more sets in Python.
You can use the union()
method that returns a new set containing all items from both sets, or the update()
method that inserts all the items from one set into another:
The union()
method returns a new set with all items from both sets:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
Try it Yourself » ExampleThe update()
method inserts the items in set2 into set1:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)
Try it Yourself »Note: Both union()
and update()
will exclude any duplicate items.
There are other methods that joins two sets and keeps ONLY the duplicates, or NEVER the duplicates, check all the built-in set methods in Python.
Track your progress - it's free!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4