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.

**What is a Set Union in Python?**

Python uses Curly braces to represent sets. 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.

Let's take a closer look at Sets from a technical standpoint. Sets are mutable, and 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.

**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.

Let 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:

**How to Find the Union of Sets in Python?**

Now that we know what are sets and what the union of sets is, let us learn how we can find the union of sets in Python. There are two implementations that allow 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**

**The union() 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 that 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 statements, namely statement 1 and statement 2, will yield the same result. Here is the 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 set union | 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. Let us understand by the example:

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}

**Python Set Union() Method on Multiple Sets**

Combining two or more sets to create a new set is one of the most beneficial actions one can conduct on sets. In Python, you can accomplish this by utilizing the union() function. Each element from the original set(s) and any extra elements given as arguments are included in the new set that the union() function returns.

The following is the syntax for utilising Python's union() method:

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

Set1 in this instance is the primary set on which we wish to conduct the union operation. To merge all sets, we can use the union() method with multiple sets (set2, set3,...) as inputs. A new set containing each distinct element from the original sets is the result of the union() method.

To further understand how the union() method functions, let's look at an example:

# Example of union() method set1 = {1, 2, 3} set2 = {3, 4, 5} set3 = {5, 6, 7} union_set = set1.union(set2, set3) print(union_set)

**Output:**

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

Three sets are present in the example above: set1, set2, and set3. All three sets have been combined using the union() method, and the outcome has been saved in the union_set variable. The unique components from each of the three sets are all present in the resultant set.

**The union() Method vs. Set union operator**

A set union operator (|) is also available in Python and can be used to unite sets. Similar to the union() method, the set union operator creates a new set that has every distinct element from the original sets.

Let's contrast the set union operator with the union() method:

# Example of set union operator set1 = {1, 2, 3} set2 = {3, 4, 5} set3 = {5, 6, 7} union_set = set1 | set2 | set3 print(union_set)

**Output:**

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

The set union operator (|) was used in the example above to merge the three sets. The unique components from each of the three sets are all present in the resultant set.

The same operation is carried out and the same outcome is produced by both the union() method and the set union operator. There is, however, one significant distinction between them. The set union operator only functions with sets, but the union() method is capable of accepting any iterable as an argument.

For instance, we can merge a set and a list using the union() method:

# Example of union() method with list set1 = {1, 2, 3} list1 = [3, 4, 5] union_set = set1.union(list1) print(union_set)

**Output:**

{1, 2, 3, 4, 5}

In the example above, we combined a set (set1) with a list (list1) using the union() method. All of the distinct components from the set and the list are included in the final set.

Although, The set union operator cannot be used with a list:

# Example of set union operator with list set1 = {1, 2, 3} list1 = [3, 4, 5] union_set = set1 | list1 print(union_set)

**Output:**

TypeError: unsupported operand type(s) for |: 'set' and 'list'

Because we are attempting to use the set union operator with a list, and this is not supported, we receive a TypeError in the example above.

**Conclusion**

Python comes with a vast number of built-in functions that can be used to execute many common set operations such as intersection and difference. We highly recommend you learn about both of these operations. The union operator of sets and its implementation in Python are described in this article.