In this Python tutorial, we’ll uncover the concept of Python set union method. We’ll explain its role and proper use in Python programs with the help of practical Python code examples.
Introduction: Python Set Union Method
Union is a method of Python set. It’s used to return a new set which contains unique and common items of all the sets used with it. It can take one or multiple arguments.
Let’s now understand this concept by using practical examples. But first, let’s try to understand its syntax.
Syntax
setA.union(one or more sets)
- Python set union method can take a single as well as multiple arguments. These arguments can be Python tuples, sets, dictionaries, lists or Python strings.
- Return value of this method is a new set that contains all the unique and common items of all the sets used.
- Passing it no argument will result in a shallow copy of set that contains items of its calling set.
Understanding Python Set Union Method (Multiple Examples)
Below examples will demonstrate the proper use of union method in Python sets.
Example 1: union() Applied on 2 Sets
i = {2,1,4,6} j = {45,3,2,8} print( i.union(j) )
Output
{1, 2, 3, 4, 6, 8, 45}
- In this program, we’ve created 2 sets.
- Then we’ve applied union method on them. We’ve called the union method of first set(i) and have passed second set(j) as an argument to it.
- We’ve used it inside print function to display the result. We can see that a new set has been displayed which contains all the unique and common items of these 2 sets.
Example 2: union() Applied on more than 2 Sets
i = {1,2,3} j = {7,8,9} k = {6,2,3} print( i.union(j,k) )
Output
{1, 2, 3, 6, 7, 8, 9}
In this program, we can see that passing 2 arguments to union method also works perfectly fine. The result shows a set which contains all the unique and common items of all these three sets.
Example 3: union() With Iterables As Arguments
a = {1,5,3} b = [7,8,2,1] c = {'a':5,'b':10} d = 'Python' print( a.union(b,c,d) )
Output
{'b', 1, 2, 3, 'a', 5, 7, 8, 'o', 'n', 't', 'h', 'P', 'y'}
- In this code, we’ve created a Python set, list, tuple, dictionary and Python string.
- Then we’ve called the union method of set(a) and have passed all these iterables as an argument to this method.
- As a result, a new set has been returned which consists of all the unique and common items.
- It means passing these iterables as an argument to this method will also work just fine.
Example 4: Passing No Arguments
setA={999,888,777} print(setA.union())
Output
{888, 777, 999}
We can see that passing no arguments to union() method results in returning a shallow copy of all the items of its calling set(set A in this case). If the calling set is an empty set then the new returned set will also be empty. An empty set is denoted by set().
Example 5: Return Value of Python Set Union Method
setX = {'w','x','y','z'} setY = {'p','w','r','y'} returnedValue = setX.union(setY) print(returnedValue)
Output
In order to see what is returned by union method, we’ve created a simple Python program. See below steps which explained how the above program works.
- First we’ve created 2 Python sets.
- Then we’ve called union method of set X and have passed set Y as an argument to it.
- We’ve created a variable which will store the value returned by union method.
- Finally, we’ve printed the result. It means union() method returns a new set which contains all the unique and common items of the sets used.
Example 6: union() Method Applied on Python List of Sets using Python For Loop
listA=[{'a','b','g','h'},{(5,4,2),9,8},{9,8,7}] listB=[{'e','f','g'},{(5,4),2,3.5},{1,4,6}] newList=[] for a,b in zip(listA,listB): newList.append(a.union(b)) print(newList)
Output
[{'h','b','a','e','g','f'}, {2,(5,4,2),3.5,8,9,(5,4)}, {1,4,6,7,8,9}]
- In the above Python code, we’ve created 2 Python list of sets and an empty Python list(it’s used to store the resulting sets) as well.
- Then we’ve specified a Python for loop which will take items of both lists during iteration. Click here to learn more about iterating over multiple Python lists simultaneously.
- Inside the body of loop, we’ve applied union method on these items. Items(sets) of first list(list A) has been used to call the union method while items(sets) of second list(list B) has been passed as an argument to it.
- We’ve added the returning value of this method inside the new list using its append method.
- Finally, we’ve printed the new list just to show the result. We can see it in the above output block.
Example 7: Find Union Using | Operator
a = {1,2,3} b = {3,4,5,6} c = {5,6,7,8,9} print(a|b) print(a|b|c)
Output
{1, 2, 3, 4, 5, 6} {1, 2, 3, 4, 5, 6, 7, 8, 9}
We can see that by using (|) operator, we can return a new set which contains unique and common items of all the specified sets. Do give this operator a try as well.
So this is how we can use Python set union method properly. If you like this post and have learned something new from it then do share it with other Python developers.
Click here to learn more about Python set methods.
Don’t hesitate to ask if you still have any questions related to Python set union method. We’ll be very happy to answer all of them.
Conclusion
In conclusion, hope you now have a clear and practical knowledge of how to properly use Python set union method. Please feel free to share your amazing feedback with us in the comment section.
Other worth reading articles on Python programming are available on this site. Feel free to visit them as well. Thank you for reading this article.