Python 3 reference · Set Methods

Python Set union() Method

Return a new set with all elements from this set and the others. The same as a | b.

Python 3 Since 3.0

Syntax

set.union(*others)

Parameters

ParameterDescription
*others One or more iterables.

Return value

A new set.

Examples

Combine setsTry it yourself
a, b = {1, 2}, {2, 3}
print(a.union(b), a | b)

Output

{1, 2, 3} {1, 2, 3}
Any iterables, several at onceTry it yourself
tags = {"python"}.union(["web"], ("api", "python"))
print(sorted(tags))

Output

['api', 'python', 'web']

Version history

VersionChange
Python 3.0Added