Skip to main content

Python Set union() Function

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() function parameters:

ParameterConditionDescription
set1, set2, ...OptionalA comma-separated list of one or more sets to merge with
note

If the argument is not passed to union(), it returns a Shallow copy of the set.

note

You can also pass iterables like list, tuple, dictionary or string. In that case, union() method first converts the iterables into sets and then computes the union.

union() Return Value

Python Set union() function 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
original_set = {1, 2, 3}

# Create a shallow copy using union() without any arguments
shallow_copy = original_set.union()

# Modify the original set
original_set.add(4)

# Display both sets
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:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a List B
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:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a Tuple B
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.

note

The dictionary keys are used for union(), not the values!

For example:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create Dictionary B and C
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:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a String B
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}

# equivalent to: A.union(B)
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}

# equivalent to result = A.union(B, C)
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}