Methods and Functions

Python Set union()

While mathematical computations we may want to club together all the data elements of multiple sets. In Mathematics, we use the Union function. It is a fundamental operation that allows sets to be merged and connected to one another. The union is represented with a ∪ symbol. Python set union() is a built-in function that provides the exact similar functionalities of the Union operation. Let us look at a basic example to understand what exactly is Union operation.

A = {1, 3, 5}

B = {2, 4, 6}

A U B = {1, 3, 5, 2, 4, 6}  (Union of Set A and Set B)

Python set union

Definition

  • The Python set union() function returns a set containing all the distinct items from the original declared set as well as all items from the specified sets.
  • Python set union() method returns a single new set by combining all the distinct elements of the specified sets.

In Layman’s terms, The union of two given sets, A and B, is a set that contains all of the items of A and all of the elements of B in such a way that no element is repeated. Let us look at how the Python set union() function is being used, it’s alternate operator along with its example programs.

Syntax of Set union()

The syntax followed by Python set union() is as follows:

                    

set1.union(set2, set3, ….setn)

Note – You can specify as many sets as you like; we simply need to separate each one of them with commas.

Python set union() Parameters

The Python set union() function accepts multiple numbers of Set iterables as its Parameters.

Return value from union()

  • The Set union() method returns a new set that contains elements from the given set as well as all other sets (passed as an argument).
  • If union() is not given an argument, it returns a shallow duplicate of the set.

Working on Set Union

The set containing all distinct elements present in all the sets formed by the union of two or more sets. As an example:

                    

A = {5, 10}
B = {5, 15, 25}
C = {50}

Then,
A U B = B U A = {5, 10, 15, 25}
A U C = C U A = {5, 10, 50}
B U C = C U B = {5, 15, 25, 50}

A U B U C = {5, 10, 15, 25, 50}

Example 1: Python Set union()

The basic functionality of the Python union() built-in set function is seen in the below Python program.

Example 1

                    

# Python program to illustrate union()
A = {1, 3, 5, 7}
B = {2, 4, 6, 8}
C = A.union(B)
print('Union of Set A and Set B:', C)

set0 = {'a', 'b'}
set1 = {'x', 'y', 'a', 'p'}
set2 = {'p', 'q', 'x', 'b'}
print('Union of all 3 sets is:', set0.union(set1, set2))

Output

                    

Union of Set A and Set B: {1, 2, 3, 4, 5, 6, 7, 8}
Union of all 3 sets is: {'a', 'p', 'q', 'y', 'b', 'x'}

Example 2

Union operation can also be applied on multiple sets by creating a chain of union() method calls. Let us understand this by looking at the following example.

                    

# Python program to illustrate union()
A = {'red', 'blue', 'white'}
B = {'red', 'green', 'black'}
C = {'pink', 'red', 'white', 'orange'}

union = A.union(B).union(C)
print(union)

Output

                    

{'red', 'blue', 'pink', 'black', 'green', 'white', 'orange'}

Example 2: Set Union using the | Operator

If we do not want to use the built-in set union function, then Python provides us with an alternate option to find out the Union of 2 sets. For this purpose, we use the ‘|’ operator.

                    

# Python program to illustrate | Operator
A = {'Sam', 'Tim', 'Peter'}
B = {'Jim', 'Kia', 'Sam', 'Betty'}
C = {'Tim', 'Kia', 'Kelly', 'Peter'}

print('A | B:', A | B)
print('A | C:', A | C)
print('B | C:', B | C)

print('A | B | C:', A | B | C)

Output

                    

A | B: {'Jim', 'Peter', 'Betty', 'Kia', 'Sam', 'Tim'}
A | C: {'Kelly', 'Peter', 'Kia', 'Sam', 'Tim'}
B | C: {'Betty', 'Peter', 'Kia', 'Tim', 'Kelly', 'Jim', 'Sam'}
A | B | C: {'Peter', 'Betty', 'Kia', 'Tim', 'Kelly', 'Jim', 'Sam'}

Difference between union() and | operator

The set union can be obtained by using both the union() method and | operator. But there is a distinction between them. Both operands must be sets when using the | operator. In contrast, the union() method will take any iterable as an input, convert it to a set, and then conduct the union. Let us look at the below example to understand the basic difference between Python set union() and the | operator.

Example

                    

num = {1, 2, 3}
name = ['Red', 'Blue', 'Green']

print('num ∪ name:', num.union(name))
print('')

print('num ∪ name:', num | name)

Output

                    

num ∪ name: {1, 2, 3, 'Blue', 'Green', 'Red'}

Traceback (most recent call last):
File "", line 6, in 
TypeError: unsupported operand type(s) for |: 'set' and 'list'

Frequently Asked Questions

Q1. How do you set a union in Python?

Python set union() is a built-in function that provides a way to set a union in the Python programming language. The Python set union() function returns a set containing all the distinct items from the original declared set as well as all items from the specified sets.

Also, If we do not want to use the built-in Set Union function, then we have an alternate option to find out the Union of 2 sets. For this purpose, we use the ‘|’ operator.

Example

                    

set1 = {'Car', 'Bike', 'Bus'}
set2 = {'Bike', 'Plane', 'Truck'}

# using union() method
print('set1 ∪ set2:', set1.union(set2))

# using | operator
print('set1 ∪ set2:', set1 | set2)

Output

                    

set1 ∪ set2: {'Bus', 'Plane', 'Bike', 'Truck', 'Car'}
set1 ∪ set2: {'Bus', 'Plane', 'Bike', 'Truck', 'Car'}

Q2. What is set() in Python?

A set is a group of elements that are not in any particular sequence. Every set element must be unique (no duplication) and immutable (cannot be changed). In Python, one of the methods for creating a set is by using the Python set() built-in function.

Example

                    

# Python program to create a set
# using set() function
names = ['Ricky', 'Daisy', 'Lily', 'John']

# creates set from list
names_set = set(names)
print('Created set:', names_set)

Output

                    

Created set: {'Lily', 'John', 'Ricky', 'Daisy'}

Q3. How do you do set operations in Python?

Set iterables can be used to perform set operations such as union, intersection, difference, and symmetric difference. This is something we can achieve using either Python operators or Python set built-in functions. Below is an example displaying each of the set operations:

Example

                    

A = {'a', 'd', 'z', 'o', 's'}
B = {'a', 'v', 'r', 's'}

print('Union: A ∪ B:', A.union(B))
print('Intersection: A ∩ B:', A.intersection(B))
print('Difference: A - B:', A.difference(B))
print('Symmetric Difference: A ^ B:', A.symmetric_difference(B))

Output

                    

Union: A ∪ B: {'v', 'a', 'r', 'z', 'o', 'd', 's'}
Intersection: A ∩ B: {'a', 's'}
Difference: A - B: {'z', 'o', 'd'}
Symmetric Difference: A ^ B: {'r', 'd', 'z', 'o', 'v'}

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.