Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

set.union

Creates a set of all members present in either input.

Version: Python 3.14; repository image, production runtime not attested

Syntax

s.union(*others)

Creates a set of all members present in either input.

Duplicates collapse even when the other input is a list. The receiver stays unchanged.

Membership in at least one input is enough for union. The example retains 2 from the receiver and adds 4 once despite its two occurrences in the list. The original still contains just 2 and 3. Intersection of these inputs would retain only their common 3.

assert {1, 2}.union([2, 3, 3]) == {1, 2, 3}

Examples

s = {2, 3}
print(sorted(s.union([3, 4, 4])))
print(sorted(s))

Result

[2, 3, 4]
[2, 3]

Membership in at least one input is enough for union. The example retains 2 from the receiver and adds 4 once despite its two occurrences in the list. The original still contains just 2 and 3. Intersection of these inputs would retain only their common 3.

Sources