Python Function Reference

set.union

Combine the set with other collections.

Python 3.13
set.union(*others)

Parameters

*othersIterable
The collections whose items go into the result.

Return value

A new set with all the items; the originals are not changed.

Examples

Union of two sets

Python 3.13
print({1, 2}.union({3, 4}))
{1, 2, 3, 4}

Union with a list and a tuple

Python 3.13
print({1, 2}.union([2, 3], (4,)))
{1, 2, 3, 4}

Details

The | operator does the same but accepts only sets, while the method accepts any iterables.

See also

Where to learn