You are currently viewing Python set union() Function

The Python set union() function is used to get the unique elements from two or multiple sets. A Python set is a data structure that does not allow duplicate values. So to merge two set’s into a single set object, use the union function which combines the multiple sets and eliminates the duplicate values. In this article, we will discuss how to perform Union operations on two or multiple sets in Python.

Advertisements

1. Quick Examples of Union Python Set

Following is a quick example of performing a union operation on set objects.


# perform union on id and cost
print("id U cost - ",id.union(cost))

# perform union on id, cost and quantity
print("id U cost U quantity - ",id.union(cost,quantity))

# perform union on id and cost
print("id | cost - ",id|cost)

# perform union on id, cost and quantity
print("id | cost | quantity - ",id|cost|quantity)

2. Python set union()

The Python union() function is used to combine two or multiple sets and the resultant set will contain only unique values (default eliminates duplicates). It will take single/multiple sets as parameters and it returns the unique elements in a new set.

2.1 Syntax of set union()

The following is the syntax of the union() function.


# syntax of union
setObj.union(setObj2,setObj3,.....)

Let’s see some examples to understand this concept better.

3. set union() Example

In this example, we will create three sets with integer elements and by using Python union(), will merge them into a single set object. To create a set in Python use {}.


# Create three sets
id={1,2,3,4,5}
cost={10,20,34,56,1}
quantity={45,6,1,2,3}

#Actual elements in the sets.
print("Set id :",id)
print("Set cost :",cost)
print("Set quantity :",quantity)

# Output
#Set id : {1, 2, 3, 4, 5}
#Set cose : {1, 34, 10, 20, 56}
#Set quantity : {1, 2, 3, 6, 45}

Let’s perform the union operation on these set objects.


# perform union on id and cost
print("id U cost - ",id.union(cost))

# perform union on id and quantity
print("id U quantity - ",id.union(quantity))

# perform union on id, cost and quantity
print("id U cost U quantity - ",id.union(cost,quantity))

# Output
#id U cost -  {1, 2, 3, 4, 5, 34, 10, 20, 56}
#id U quantity -  {1, 2, 3, 4, 5, 6, 45}
#id U cost U quantity -  {1, 2, 3, 4, 5, 34, 6, 10, 45, 20, 56}

Explanation:

  1. We returned only unique elements from id and cost sets.
  2. We returned only unique elements from id and quantity sets.
  3. Finally we are also returning unique elements from all three sets with union() function.

4. set union() with String Values

In this example, we will create three Python set objects with string elements and by using union() combine all elements from specified sets without duplicates.


# Create four sets
village1={"fields","plants","electricity","houses"}
village2={"fields","plants","electricity","houses","buildings"}
village3={"None"}

# Actual elements in the sets.
print("Set-1",village1)
print("Set-2",village2)
print("Set-3",village3)

# Output
# Set-1 {'electricity', 'fields', 'houses', 'plants'}
# Set-2 {'electricity', 'fields', 'buildings', 'houses', 'plants'}
# Set-3 {'None'}

Let’s perform the union on this set of strings.


# perform union on village1 and village2
print("village1 U village2 - ",village1.union(village2))

# perform union on village2 and village3
print("village2 U village3 - ",village2.union(village3))

# Output
#village1 U village2 -  {'electricity', 'fields', 'buildings', 'houses', 'plants'}
#village2 U village3 -  {'buildings', 'None', 'electricity', 'fields', 'houses', 'plants'}

Explanation:

  1. We returned only unique elements from village1 and village2 sets.
  2. We returned only unique elements from village2 and village3 sets.

5. Python Set Union using | operator

We can also perform set union using the | operator in Python. This we can call as “or” operator.

Syntax:


# Syntax
set1|set2|set3|.....

Let’s see some examples to understand this concept better.

5.1 Using | with Examples

In this example, we will create three sets with integer elements and return the common elements.


# Create three sets
id={1,2,3,4,5}
cost={10,20,34,56,1}
quantity={45,6,1,2,3}

# Actual elements in the sets.
print("Set-1",id)
print("Set-2",cost)
print("Set-3",quantity)

# Output
#Set-1 {1, 2, 3, 4, 5}
#Set-2 {1, 34, 10, 20, 56}
#Set-3 {1, 2, 3, 6, 45}

Output:



# perform union on id and cost
print("id | cost - ",id|cost)

# perform union on id and quantity
print("id | quantity - ",id|quantity)

# perform union on id, cost and quantity
print("id | cost | quantity - ",id|cost|quantity)

# Output
#id | cost -  {1, 2, 3, 4, 5, 34, 10, 20, 56}
#id | quantity -  {1, 2, 3, 4, 5, 6, 45}
#id | cost | quantity -  {1, 2, 3, 4, 5, 34, 6, 10, 45, 20, 56}

Explanation:

  1. We returned only unique elements from id and cost sets.
  2. We returned only unique elements from id and quantity sets.
  3. Finally, we are also returning unique elements from all three sets with “|” operator.

5.2 Using | with set of Strings

In this example, we will create three sets with string elements and return the common elements.


# Create four sets
village1={"fields","plants","electricity","houses"}
village2={"fields","plants","electricity","houses","buildings"}
village3={"None"}

# Actual elements in the sets.
print("Set-1",village1)
print("Set-2",village2)
print("Set-3",village3)

# Output
#Set-1 {'electricity', 'fields', 'houses', 'plants'}
#Set-2 {'electricity', 'fields', 'buildings', 'houses', 'plants'}
#Set-4 {'None'}

Output:


# perform union on village1 and village2
print("village1 | village2 - ",village1|village2)

# perform union on village2 and village3
print("village2 | village3 - ",village2|village3)

# Output
#village1 | village2 -  {'electricity', 'fields', 'buildings', 'houses', 'plants'}
#village2 | village3 -  {'buildings', 'None', 'electricity', 'fields', 'houses', 'plants'}


Explanation:

  1. We returned only unique elements from village1 and village2 sets.
  2. We returned only unique elements from village1 and village2 sets.
  3. We returned only unique elements from village2,village3 and village4 sets.
  4. Finally, we are also returning unique elements from all four sets with “|” operator.

6. Differences between union() and | operator

Though both union() and | operator is used to combine the set in Python, they both have some differences. Below I have covered some differences.

union()| Operator
union() is a method| is an operator
union() method explicitly fix the priority of the sets| operator will have specific operator precedence of the sets
union() is used only with sets| operator can be used with other data structures

For more examples of sets refer to Python set Methods and Python set Operators.

7. Conclusion

In this article, you have learned how to return only unique elements from multiple Python set objects using union() function and | operator with two different examples. It can be possible to apply union() and | operator on two or multiple sets.