In this python tutorial, we will discuss the union of sets and what is the union in Python. We will also check:
- Union of two sets in Python
- Union of multiple sets in Python
- Union list of sets in Python
- Union of many sets in Python
- Union of several sets in Python
- Union of all sets in a list in Python
- Union of sets python example
- Union of lists Python
What is the union?
The Union() is a method in Python that returns a new set which contains distinct items from all the set.
Syntax:
union set = set1.union(set2)
Union of two sets in Python
Lets us see, how to perform union operation on two sets in python.
- In this example, I have taken two sets as chocolates and fruits these sets contain elements.
- To perform the union operation on these sets we have to create a new set called chocolates_fruits. To create a new set we have to use chocolates_fruits = chocolates.union(fruits).
- New set chocolates_fruits is created by calling union() and fruits set is passed as an argument is in the union() method.
Example:
chocolates = {'munch','kitkat','diarymilk'}
fruits = {'mango','orange','pineapple'}
chocolates_fruits = chocolates.union(fruits)
print(chocolates_fruits)
To get the output as a combination of all the items from two sets in a new set called chocolate_fruits. We have to use print(chocolates_fruits). The below screenshot shows the output.
Read: How to add dropdown navbar in Django
Union of multiple sets in Python
Here, we can see how to perform union operation on multiple sets in python.
- In this example, I have taken multiple sets they are chocolates, fruits, vegetables, and ice_cream.
- To perform the union operation on these sets. New set chocolates_fruits is created, to create a new set we have to use chocolates_fruits = chocolates.union(fruits,vegetables,ice_cream).
- union() method is called and passed all the set as the arguments in the union() method.
syntax:
union set = set1.union(set2,set3,set4)
Example:
chocolates = {'munch','kitkat','diarymilk'}
fruits = {'mango','orange','pineapple'}
vegetables = {'tomato','chilli','potato','peas'}
ice_cream = {'vanilla','candy','blackcurrent'}
chocolates_fruits = chocolates.union(fruits,vegetables,ice_cream)
print(chocolates_fruits)
To get the output as a combination of all the items from multiple sets like chocolates = {‘munch’,’kitkat’,’diarymilk’}, fruits = {‘mango’,’orange’,’pineapple’}, vegetables = {‘tomato’,’chilli’,’potato’,’peas’}, ice_cream = {‘vanilla’,’candy’,’blackcurrent’} in a new set called chocolate_fruits, we have to use print(chocolates_fruits). You can see the output in the below screenshot.
Union list of sets in Python
Now, we can see how to perform union operationson list of sets in python
- In this example, I have created a list in Python and named it as items, and assigned some sets in the list.
- Create a new set by using the set() and call the union(), and then the items set is passed as an argument in the union() method.
- The list is unpacked by using an asterisk *.
Example:
items = [{'chocolate','biscuits'}, {'potato','tomato'}, {'apple','orange','pear'}]
print(set().union(*items))
To get the output as combination of all the items from set like [{‘chocolate’,’biscuits’}, {‘potato’,’tomato’}, {‘apple’,’orange’,’pear’}] in a new set, we have to use print(set().union(*items)). You can see the output in the below screenshot .
Read: How to Add Elements in a Set in Python
Union of many sets Python
Here, we can see how to perform union operation on many sets in python
- In this example, I have taken many sets in a list and named a list as Numbers
- Create a new set by using set() and then call the union() method, and pass the argument Number in the union() method.
- The list is unpacked by using asterisk *
Example:
Numbers = [{2,4,6}, {1,3,5}, {0}, {3,6,9},{4,8,12}]
print(set().union(*Numbers))
To get the output as combination of all the items from set like [{2,4,6}, {1,3,5}, {0}, {3,6,9},{4,8,12}] in a new set, we have to use print(set().union(*Numbers)). Below screenshot shows the output.
Union of several sets Python
Now, we can how to perform union operation on several set in python
- In this example, I have taken several sets and named them as Even_number, Odd_number, Multiple_of_3, and Multiple_of_4.
- To create new set called Numbers. We have to use Numbers = Even_number.union(Odd_number,Multiple_of_3,Multiple_of_4).
- To perform union operation on all these sets. A new set is created as Numbers and in the new set union() is called and all the sets are passed as arguments in the union() method.
Example:
Even_number = {2,4,6,8}
Odd_number = {1,3,5,7}
Multiple_of_3 = {3,6,9,12}
Multiple_of_4 = {4,8,12,16}
Numbers = Even_number.union(Odd_number,Multiple_of_3,Multiple_of_4)
print(Numbers)
To get the output as a combination of all the items from several sets like Even_number = {2,4,6,8}
Odd_number = {1,3,5,7}, Multiple_of_3 = {3,6,9,12}, Multiple_of_4 = {4,8,12,16} in a new set called Numbers, we have to use print(Numbers). You can refer to the below screenshot for the output.
Union of all sets in a list Python
Let us see, how to perform union operation on all the sets in the list in python.
- In this example, I have taken a list named Numbers.
- We have to create a new set by using set = frozenset().union(*Numbers). To create a new set, frozenset() is called, and also union() is called. And then pass the argument Numbers in the union() method.
- The frozenset() is a function that returns all the set objects from the list. An asterisk * is used to unpack the list.
Example:
Numbers = [set([2,4,6,8]), set([1,3,5,7]), set([0])]
set = frozenset().union(*Numbers)
print(set)
To get the output as a combination of items from the sets like [set([2,4,6,8]), set([1,3,5,7]), set([0])] in a new set. We have to use print(set). Below screenshot shows the output.
Union of sets Python example
Now, we can see an example on union of sets in python.
- In this example, I have taken two set vowels and consonants.
- To get all the letters in the new list called alphabets. Union operation is performed by using alphabets = vowels.union(consonants).
Example:
vowels = {'a','e','i','o','u'}
consonants = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'}
alphabets = vowels.union(consonants)
print(alphabets)
To get the output as an alphabet set. We have to use print(alphabets). You can see the output in the below screenshot.
Union of lists Python
Here, we can see how to perform union operation of lists in Python.
- In this example, I have taken two lists as list1 and list2.
- To create a new list, we have to use result =(set().union(list1,list2)).
- The new set result is created by calling set() and also by calling union() and then list1, list2 are passed as arguments in the union() method.
Example:
list1 = [2,4,6,8,10]
list2 = [1,2,3,4,5,7]
result =(set().union(list1,list2))
print(result)
To get the output as a new list, we have to use print(result). You can refer to the below screenshot for the output.
You may like the following Python tutorials:
- Introduction to Python Interface
- How to convert a String to DateTime in Python
- Escape sequence in Python
- Python list comprehension lambda
- Send email using Python
- Working with JSON data in Python
- Python – stderr, stdin and stdout
- Python get an IP Address
- Increment and Decrement operators in Python
- Constructor in Python
- How to convert dictionary to JSON in Python
In this Python tutorial, we have learned about the Union of sets and what is a union in Python. Also, We covered these below topics:
- Union of two sets python
- Union of multiple sets python
- Union list of sets python
- Union of many sets python
- Union of several sets python
- Union of all sets in a list python
- Union of sets python example
- Union of lists python
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.