union() in Python
Overview
We can use a method called union in python and set union operator, i.e. |, to get the union of two or more sets. The set union operator only works with sets, but the set union() method can be used with any iterable, like strings, lists, and dictionaries.
Syntax of union() in Python
The syntax of the union method is quite simple. The union method takes sets as parameters.
set1 is required as set1 will be iterable to unify the items. The other sets, such as set2, set3... are optional.
Note: The syntax of | or union operator is:
Parameters of union() in Python
As discussed above, the union method takes sets in the parameters. The union method can take any number of sets (two or more) as parameters. Using these parameters, the union will generate a set of elements in all the given sets.
Return Values of union() in Python
Using the sets passed as parameters to the union method, the union will generate a set and returns the generated set. The returned set consists of all the elements from both sets, but duplicates are not included (because union means the common elements only).
Exceptions of union() in Python
Usually, the union method() does not raise an error if we use the correct syntax. The union() function returns a set, which has the union of all sets(set_1, set_2, set_3 …) with set_1. If we do not pass any set in the parameter of the union() method, the union() method returns a copy of the calling set itself, i.e. set_1.
Example of union() in Python
Let's take an example of two sets and try to find their union using python's union() method.
- Finding union in python using union() method:
Output:
- Finding union in python using union operator(|):
Output:
what is union in python?
The union of two sets is nothing but a set consisting of all the distinct elements of the two given sets. To find union in python, we have two ways:
- using union() method for finding union in python
- using | operator for finding union in python
Both the union() method and | operator have the same performance. The only difference between the operator and method is that the set union operator only works with sets. In contrast, the set union() method can be used with any iterable, like strings, lists, and dictionaries.
Working of Set union in python
Suppose we are provided with two sets of values. So, the union of both sets is nothing but a set consisting of all the distinct elements of the two given sets. Refer to the diagram given below to have a better understanding. In the example below, the distinct elements of both sets are 1, 2, 3, 4, and 5. So, our answer is a set of {1, 2, 3, 4, 5}. We can also observe that 3 came in both the given sets, but in the output we have only one 3 because union means only distinct elements (or no repetitive elements).
More Example
Let us take an example of more than two sets to understand syntax better and work on the union() method in python and the union operator in python.
Unify more than 2 sets:
Output:
Using python union operator:
Output:
We can see in the code above, the set_1 and set_2 have 'a', 'b', and 'c' distinct elements. The set_2 and set_3 have 'c', 'b', 'd', 'g', and a distinct elements. So, When the union of set_1, set_2, and set_3 will be done, {'d', 'g', 'b', 'c', 'a'} will be returned.
:::
Conclusion
- The union() method in python and set union operator i.e. | is used to get the union of two or more sets.
- The syntax of union() method is:
The set union() method can be used with any iterable, like strings, lists, and dictionaries.
- The set union operator only works with sets. The syntax of | or union operator is:
- The union() method can take any arbitrary number of sets (two or more sets) as parameters.
- The union() method returns a set of distinct elements (or no repetitive elements).