Python Set union() Method

Example

Returns a set containing all items from both sets, with duplicates excluded:

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.union(y) 
print(z)

Run Instance

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 like.

Sets are separated by commas.

More Examples

Example

Integrate two or more sets:

x = {"a", "b", "c"}
y = {"f", "d", "a"}
z = {"c", "d", "e"}
result = x.union(y, z) 
print(result)

Run Instance