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
| Parameter | Description |
|---|---|
*others |
One or more iterables. |
Return value
A new set.
Examples
a, b = {1, 2}, {2, 3}
print(a.union(b), a | b)
Output
{1, 2, 3} {1, 2, 3}tags = {"python"}.union(["web"], ("api", "python"))
print(sorted(tags))
Output
['api', 'python', 'web']
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
set.update()· Add all elements from one or more iterables to the set, in place. The same as a |= b.set.intersection()· Return a new set with the elements common to this set and all others. The same as a & b.set.difference()· Return a new set with the elements of this set that are not in the others. The same as a - b.- Python Sets · tutorial