Home / Python / Reference / Set methods

Python set union() method

Return everything in either set.

Syntax

set.union(*others) / a | b

Parameters

*othersother sets or iterables

Return value

a new set

Example

Code

a = {1, 2, 3}
b = {3, 4}
print(a | b, a.union(b))
print(a.union([7], (8,)))

Output

{1, 2, 3, 4} {1, 2, 3, 4}
{1, 2, 3, 7, 8}

Related

Learn it step by step in the free lesson Dictionaries & Sets, with exercises that check your answers.