Python Set union() Method

Returns the union of two or more sets.

Syntax

Python
set.union(set1 [, set2, ...])

Parameters

ParameterDescription
set1 [, set2, ...] Required. One or more sets that will be used to create a new set based on the union of the sets' items.

Example

Python
desktopComponents = {'memory', 'processor', 'hard drive'}
laptopComponents = {'memory', 'processor', 'SSD', 'camera', 'keyboard'}
greatComputer = desktopComponents.union(laptopComponents)
print(greatComputer)

Output

{'camera', 'memory', 'hard drive', 'keyboard', 'processor', 'SSD'}