Set Methods

set.union()

Combine the receiver with every supplied iterable, deduplicating equal members. Returns a set result without changing the receiver. For an exact mutable set, the result is a separate outer set. The method accepts iterable inputs such as lists or generators, unlike the built-in | operator between sets. With zero arguments, the result has the same members as the receiver. Elements used in hashing must be hashable. Input iteration can raise exceptions; no ordering is guaranteed.

Formal Syntax & Structure

python syntaxpython
items.union(*others)

Parameters & Attributes

NameTypeDescription
othersIterable[Hashable]Zero or more iterables of elements; each iterable is supplied as a separate positional argument.

Return Value

set

set containing the calculated members.

Working Example

Example Snippetpython
a = {"pen", "book"}b = a.union(["book", "bag"], ["map"])print(sorted(b))print(sorted(a))
Python Reference