What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Python Set Union with codes and Examples

  • Mar 20, 2022
  • 5 Minutes Read
Python Set Union with codes and Examples

A major topic of mathematics is set theory, which entails storing a group or collection of linked components. Python chose to make the implementation of mathematical sets an intrinsic data structure because of its importance. We'll learn how to find the union between two sets using the python set union() method in this article. But first, let's review what sets are and what we mean when we say "Union of two sets".

What is a Set in Python?

Curly braces are used to represent sets, which are one of Python's four major data structures. Sets, like lists, can store any type of data (strings, integers, floats), as well as a mix of data types. However, it makes logical sense to group together only related things in a set. You might be wondering what the difference is between dictionary and set data structures since they both utilize curly braces to describe themselves. The solution is simple: dictionaries store data in the form of key-value pairs, whereas sets do not.

Let's take a closer look at Sets from a technical standpoint. Sets are mutable, iterable, and do not allow duplicate elements to exist within them. It's vital to note that because sets are unordered, we can't use indexes to access their elements as we do with lists. In fact, when compared to other data structures, Sets offer a significant advantage. Internally, sets use a data structure called a hash table, which provides a highly optimal technique for determining whether or not a certain element is in the set. This approach is faster than the most efficient search algorithm. Let's have a look at some Python sets examples.

For example:

setA = { "a", "b", "c"}
print(setA)
# Adding element to the set
setA.add("d")
print(setA)

 

Output:

{'c', 'b', 'a'}
{'c', 'd', 'b', 'a'}

 

What is the Union of Sets?

The union of any two or more sets yields a fully new set that comprises a mixture of elements from all the sets. In other words, The union of two or more sets is the set of all distinct elements present in all the sets. The word 'or' is used to represent the union of sets. Consider the two sets A and B. All of the elements that are present in either A or B or both sets, will be present in the union of A and B. The union of sets is represented by the symbol ‘U’. The set operation, often known as the union of sets, is written as:

A ∪ B = {x: x ∈ A or x ∈ B}. Here, x is the element present in both sets, A and B.

 

Lets us understand this concept better with an example:

If A = { 23, 453, 43, 66, 21} and B = { 43, 785, 12, 35, 23, 545}

Then A U B = {23, 453, 43, 66, 21, 43, 785, 12, 35, 545}

 

You can also visualize this concept using a Venn diagram, as shown below:

Union of two sets P and Q

How to Find the Union of Sets in Python?

Now that we know what are sets and what is the union of sets is, let us now learn how we can find the union of sets in python. Luckily for us, Python provides us with two implementations that allow in finding the union: one is an inbuilt function and another is an overridden operator. So, without any further due, let us look into each of these approaches one by one.

1) Using Set.Union() method

This is an inbuilt method in python meant for finding the union of any given number of sets. This function implements the union operator and requires a set upon which the function can be called. The other sets are passed as arguments to the function. The value returned by this function is a new set which is the union of the set upon which the function is called and all the sets which are passed an argument. . Please note that this function doesn’t make changes to the original sets.

For example, suppose we are given two sets ( A and B) and we want to find AUB. So, the syntax will look something like this

A.union(B) # Statement 1
B.union(A) # Statement 2

 

Note that both the statements, namely statement 1 and statement 2, will yield the same result. Let us now look at code to further understand the use of this function. Notice how we have used it to find the union of more than two sets.

Python Code:

A = {1, 2, 3, 4} #Set A
B = {2, 5, 6, 7} #Set B
C = {7, 8, 9, 10} #Set C
D = {11, 12, 13, 14} #Set D

# E = A U B
E = A.union(B) # Statement 1

# F = A U B U C U D
F = A.union(B, C, D) #Statment 2

print("A U B: " +str(E))
print("A U B U C U D: " +str(F))

 

Output:

A U B: {1, 2, 3, 4, 5, 6, 7}
A U B U C U D: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}

 

2) Using | operator

This method does exactly what the previous inbuilt function does. Here, instead of a function call we use an operator, which is |. Look at the code below the union of two or more sets can be found using this operator,

Python Code:

A = {1, 2, 3, 4} #Set A
B = {2, 5, 6, 7} #Set B
C = {7, 8, 9, 10} #Set C
D = {11, 12, 13, 14} #Set D

# E = A U B
E = A|B 
# F = A U B U C U D
F = A|B|C|D 

print("A U B: " +str(E))
print("A U B U C U D: " +str(F))

 

Output:

A U B: {1, 2, 3, 4, 5, 6, 7}
A U B U C U D: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}

 

Conclusion

Python is noted for its simple syntax, which is similar to that of English. Python comes with a vast number of built-in functions that can be used to execute many common set operations. The union operator of sets and its implementation in Python were described in this article. There are other two major set operations intersection and difference. We highly recommend you to learn about both these operations.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Riddhima Agarwal
Hey, I am Riddhima Agarwal, a B.tech computer science student and a part-time technical content writer. I have a passion for technology, but more importantly, I love learning. Looking forward to greater opportunities in life. Through my content, I want to enrich curious minds and help them through their coding journey