Python Set union() Method
Instance
Returns a set containing all items from both sets, duplicates are excluded:
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} z = x.union(y) print(z)
Definition and usage
The union() method returns a set that contains all items from the original set and all items from the specified set.
You can specify any number of sets, separated by commas.
If an item exists in multiple sets, it appears only once in the result.
Syntax
set.union(set1, set2 ...)
Parameter value
Parameter | Description |
---|---|
set1 | Required. The set to be integrated. |
set2 |
Optional. The set to be integrated. You can compare as many sets as you want. Sets are separated by commas. |
More instances
Instance
Integrating more than two sets:
x = {"a", "b", "c"} y = {"f", "d", "a"} z = {"c", "d", "e"} result = x.union(y, z) print(result)