Python

How to use union on python set

The unordered collection of items is called set in Python. Any item can be added or removed from the set but the value of any item in set is not changeable like a tuple. Every item in the set must be unique. Set does not contain any index like list or tuple, so each item of the set can’t be accessed by index like list or tuple. Sets are mainly used for different types of mathematical operations in Python like union, intersection, difference, etc. Different symbols are used to perform different types of operations. Pipe ( | ) is used for union operation on sets. How to declare sets and perform union operation on them are explained in this tutorial.

Prerequisite:

Before starting this tutorial, it is essential to clear the concept of union operation. A new set is formed by combining the common and uncommon items of two or more sets by using union operation. Generally, ‘U’ symbol is used to indicate the union operation.

For example:

There are two sets, A and B.

Set, A = { 79, 34, 99, 23, 61 }
Set, B = { 26, 99, 61, 55 }
Then, AUB = { 34, 99, 55, 23, 26, 61, 79 }

The graphical representation of the above union operation is shown below. Here, 99 and 61 are common in both sets.

Example-1: Apply union operation on the sets of numeric data

In the following example, two numeric sets, A and B are defined. Three values are common in these sets. These are 7, 9, and  11.

#!/usr/bin/env python3

# Define two sets, A and B
A = { 10, 20, 7 ,9, 11, 15 }
B = { 11, 3, 7, 9, 25 }

# The union output of A and B
print("The output of A U B is :\n", A | B)

Output:

The following output will appear after running the script.

Example-2: Apply union operation on the sets of character data

The following example shows the use of union operators on three character sets. The sets are A, B and C. Here, set A contains four characters, set B contains three characters, and set C contains four characters. Two characters, ‘C’ and ‘R’ exists in all three sets. So, the new set after union operation will contain, 4+1+2=7 characters.

#!/usr/bin/env python3

# Define three sets of characters, A, B and C
A = { 'A', 'C', 'E' ,'R' }
B = { 'B','C', 'R' }
C = { 'C','G', 'R', 'X' }

# The union output of A, B, and C
print("The output of (A U B U C)is :\n", A | B | C)

Output:

The following output will appear after running the script.

Example-3: Apply union operation on the sets of string data

How the union operation can be done on two sets of string data and iterate the values of the final set after union operation using loop are shown in the following example. Here, two sets, A and B contains the name of persons. After applying union on these sets, the result is stored in the variable, C.  Next, for loop is used to iterate the values of the set C like list or tuple and print the value in each line.

#!/usr/bin/env python3

# Define two sets of strings, A and B
A = { 'Joya Hasan', 'Ahmed Ali', 'Eella Nazir' ,'Rita Hossain' }
B = { 'Mehr Afroz','Ahmed Ali', 'Rita Hossain', 'Zinnia Rahman' }

# Apply union operation on A and B, store the result into C
C = A | B

# Print Set A
print("Set A:", A)

# Print Set B
print("\nSet B:", B)

# Print message
print("\nThe items after applying UNION:\n")

# Iterate each item of the set C
for val in C:
  # Print each item
  print(val)

Output:

Here, both sets contain four values and two values are common in sets A and B. These are ‘Ahmed Ali’ and ‘Rita Hossain’. So, set C will contain six values. The following output will appear after running the script.

Example-4: Apply union operation using union() method

union() method can be used as an alternative of ‘|’ operator to perform union operations among the sets. In the following script, two sets of string values are defined by A and B. ‘PHP’ and ‘Laravel’ values are common in both sets. The variable, C contains the set after applying union operation using union() method. The values of the three sets are printed here and for loop is used to iterate the values of set C like the previous example.

#!/usr/bin/env python3

# Define two sets of strings, A and B
A = { 'PHP', 'Java', 'Laravel' ,'C#' }
B = { 'Angular','PHP', 'Javascript', 'Laravel' }

# Apply union() method on A and B, store the result into C
C = A.union(B)

# Print Set A
print("Set A:", A)

# Print Set B
print("\nSet B:", B)

# Print message
print("\nThe items after applying union() method:\n")

# Iterate each item of the set C
for val in C:
  # Print each item
  print(val)

Output:

After applying the union operation, set C will contain six values. These are, ‘Java’, ‘Angular’, ‘Javascript’, ‘PHP’, ‘Laravel’ and ‘C#’. The following output will appear after running the script.

Conclusion:

When you work with python sets and need to merge the data of the sets by removing duplicate values then you will require to perform union operation on the sets. Union operator( | ) or union()  method can be used to do the task. If you want to retrieve the common data only from the sets then you have to perform intersection operation that is not discussed here. There are many other options exist in python like this to do different types of mathematical tasks in python sets. I hope this tutorial will help the readers to understand the concept of union operation and apply it in their script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.