Skip to content

Python Set union() Method

How to use Python's Set union() method to combine elements from multiple sets, iterables, or the set itself while preserving uniqueness.

The Set union() method returns a new set containing all unique elements from two or more sets.

Syntax

my_set.union(set1, set2, ...)

union() Parameters

Python Set union() method parameters:

Parameter Condition Description
set1, set2, ... Optional A comma-separated list of one or more sets to merge with

union() Return Value

Python Set union() method returns a new set with elements from the set and all other sets passed as argument.

Example

Example 1: Union of Two Sets with union() method

A = {'Tom', 'Anna'}
B = {'David', 'Ryan'}

result = A.union(B)

print(result) # Output: {'Ryan', 'Anna', 'Tom', 'David'}

output

{'Ryan', 'Anna', 'Tom', 'David'}

Example 2: Union between Multiple Sets

The union() method allows to compute the union of elements from multiple sets:

A = {1, 2, 3}
B = {3, 4, 5, 6, 7}
C = {6, 7, 8, 9, 10}

result = A.union(B, C)

print(result) # Output: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

output

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Example 3: Shallow Copy with union()

When called without any arguments, union() returns a new set that contains all the elements of the original set.

This behavior is effectively a shallow copy operation because it duplicates the set without creating a deep copy of the elements themselves.

original_set = {1, 2, 3}

shallow_copy = original_set.union()

original_set.add(4)

print("Original Set:", original_set) # Output: Original Set: {1, 2, 3, 4}
print("Shallow Copy:", shallow_copy) # Output: Shallow Copy: {1, 2, 3}

output

Original Set: {1, 2, 3, 4}
Shallow Copy: {1, 2, 3}

Example 4: Union between a Set and a List

You can also pass iterables like list: union() first converts the iterables to sets and then computes the union.

For example:

A = {'a', 'e', 'i', 'o', 'u'}

B = ['d', 'e', 'f']

result = A.union(B)

print(result) # Output: {'a', 'i', 'u', 'f', 'e', 'd', 'o'}

output

{'a', 'i', 'u', 'f', 'e', 'd', 'o'}

Example 5: Union between a Set and a Tuple

You can also pass iterables like tuple: union() first converts the iterables to sets and then computes the union.

For example:

A = {'a', 'e', 'i', 'o', 'u'}

B = ('d', 'e', 'f')

result = A.union(B)

print(result) # Output: {'a', 'i', 'u', 'f', 'e', 'd', 'o'}

output

{'a', 'i', 'u', 'f', 'e', 'd', 'o'}

Example 6: Union between a Set and a Dictionary

You can also pass iterables like dictionary: union() first converts the iterables to sets and then computes the union.

For example:

A = {'a', 'e', 'i', 'o', 'u'}

B = {1: 'd', 2: 'e', 3: 'f'}
C = {'d': 1, 'e': 2, 'f': 3}

print(A.union(B)) # Output: {'u', 1, 2, 3, 'a', 'e', 'o', 'i'}
print(A.union(C)) # Output: {'u', 'd', 'f', 'a', 'e', 'o', 'i'}

output

{'u', 1, 2, 3, 'a', 'e', 'o', 'i'}
{'u', 'd', 'f', 'a', 'e', 'o', 'i'}

Example 7: Union between a Set and a String

You can also pass iterables like string: union() first converts the iterables to sets and then computes the union.

For example:

A = {'a', 'e', 'i', 'o', 'u'}

B = 'def'

print(A.union(B)) # Output: {'a', 'i', 'u', 'f', 'e', 'd', 'o'}

output

{'a', 'i', 'u', 'f', 'e', 'd', 'o'}

Equivalent Operator | for union()

Set Union can be performed with the | operator as well, providing an alternative to the union() method.

A = {1, 2, 3, 4, 5}
B = {3, 4, 5, 6, 7}

result = A | B

print(result) # Output: {1, 2, 3, 4, 5, 6, 7}

output

{1, 2, 3, 4, 5, 6, 7}

Moreover, you can use the | operator with multiple sets:

A = {1, 2, 3}
B = {3, 4, 5, 6, 7}
C = {6, 7, 8, 9, 10}

result = A | B | C

print(result) # Output: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

output

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Navigation