Python – Find union of multiple sets
Last Updated :
01 Feb, 2023
Given multiple sets list, the task is to write a Python program to find union of each set.
Examples:
Input : test_list = [{4, 3, 5, 2}, {8, 4, 7, 2}, {1, 2, 3, 4}, {9, 5, 3, 7}]
Output : {1, 2, 3, 4, 5, 7, 8, 9}
Explanation : All elements from all sets included. Duplicates removed.
Input : test_list = [{4, 3, 5, 2}, {8, 4, 7, 2}, {1, 2, 3, 4}]
Output : {1, 2, 3, 4, 5, 7, 8}
Explanation : All elements from all sets included. Duplicates removed.
Method #1 : Using union() + * operator
In this, we perform task of getting union using union(), and * operator is used to perform task of packing all the sets together.
Python3
test_list = [{ 4 , 3 , 5 , 2 }, { 8 , 4 , 7 , 2 }, { 1 , 2 , 3 , 4 }, { 9 , 5 , 3 , 7 }]
print ( "The original list is : " + str (test_list))
res = set ().union( * test_list)
print ( "Multiple set union : " + str (res))
|
Output
The original list is : [{2, 3, 4, 5}, {8, 2, 4, 7}, {1, 2, 3, 4}, {9, 3, 5, 7}]
Multiple set union : {1, 2, 3, 4, 5, 7, 8, 9}
Time Complexity: O(n*m) where n is the number of sets in the test_list and m is the average number of elements in each set.
Auxiliary Space: O(m), where m is the average number of elements in each set. This is because the program creates a new set, that has the union of all the elements in the sets in test_list, and the space occupied by this set is m.
Method #2 : Using chain.from_iterable() + * operator
In this, we perform task of union, which in turn is flattening using from_iterable().
Python3
from itertools import chain
test_list = [{ 4 , 3 , 5 , 2 }, { 8 , 4 , 7 , 2 }, { 1 , 2 , 3 , 4 }, { 9 , 5 , 3 , 7 }]
print ( "The original list is : " + str (test_list))
res = set (chain( * test_list))
print ( "Multiple set union : " + str (res))
|
Output
The original list is : [{2, 3, 4, 5}, {8, 2, 4, 7}, {1, 2, 3, 4}, {9, 3, 5, 7}]
Multiple set union : {1, 2, 3, 4, 5, 7, 8, 9}
Method #3 : Using Counter() function
Python3
from collections import Counter
from itertools import chain
test_list = [{ 4 , 3 , 5 , 2 }, { 8 , 4 , 7 , 2 }, { 1 , 2 , 3 , 4 }, { 9 , 5 , 3 , 7 }]
print ( "The original list is : " + str (test_list))
res = list (Counter(chain( * test_list)).keys())
res.sort()
print ( "Multiple set union : " + str ( list (res)))
|
Output
The original list is : [{2, 3, 4, 5}, {8, 2, 4, 7}, {1, 2, 3, 4}, {9, 3, 5, 7}]
Multiple set union : [1, 2, 3, 4, 5, 7, 8, 9]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using update() method
Python3
test_list = [{ 4 , 3 , 5 , 2 }, { 8 , 4 , 7 , 2 }, { 1 , 2 , 3 , 4 }, { 9 , 5 , 3 , 7 }]
print ( "The original list is : " + str (test_list))
res = set ()
for i in test_list:
res.update(i)
print ( "Multiple set union : " + str ( list (res)))
|
Output
The original list is : [{2, 3, 4, 5}, {8, 2, 4, 7}, {1, 2, 3, 4}, {9, 3, 5, 7}]
Multiple set union : [1, 2, 3, 4, 5, 7, 8, 9]
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #5: Using reduce() method:
Python3
from functools import reduce
test_list = [{ 4 , 3 , 5 , 2 }, { 8 , 4 , 7 , 2 }, { 1 , 2 , 3 , 4 }, { 9 , 5 , 3 , 7 }]
print ( "The original list is : " + str (test_list))
res = reduce ( set .union, test_list)
print ( "Multiple set union : " + str (res))
|
Output
The original list is : [{2, 3, 4, 5}, {8, 2, 4, 7}, {1, 2, 3, 4}, {9, 3, 5, 7}]
Multiple set union : {1, 2, 3, 4, 5, 7, 8, 9}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using set.update() method
Python3
test_list = [{ 4 , 3 , 5 , 2 }, { 8 , 4 , 7 , 2 }, { 1 , 2 , 3 , 4 }, { 9 , 5 , 3 , 7 }]
print ( "The original list is : " + str (test_list))
result = set ()
for s in test_list:
result.update(s)
print ( "The union of multiple sets:" , result)
|
Output
The original list is : [{2, 3, 4, 5}, {8, 2, 4, 7}, {1, 2, 3, 4}, {9, 3, 5, 7}]
The union of multiple sets: {1, 2, 3, 4, 5, 7, 8, 9}
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Find the Union on List of Sets in Python
The union of sets involves combining distinct elements from multiple sets into a single set, eliminating duplicates. In this article, we will study how to find the union on a list of sets in Python. Find the Union on a List of Sets in PythonBelow are some of the ways by which we can find the union o
4 min read
Python - Symmetric Difference of Multiple sets
Symmetric Differences among groups of sets are elements that belong to any one of the sets but are not present in any other set. Given a list of sets and the task is to write a Python program to get the symmetric difference of the same. Input : test_list = [{5, 3, 2, 6, 1}, {7, 5, 3, 8, 2}, {9, 3},
3 min read
Find the length of a set in Python
We are given a set and our task is to find its length. For example, if we have a = {1, 2, 3, 4, 5}, the length of the set should be 5. Using len()len() function in Python returns the number of elements in a set. It provides total count of unique items present in set. [GFGTABS] Python a = {1, 2, 3, 4
2 min read
Find the size of a Set in Python
A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements. Pythonâs set class represents the mathematical notion of a set. The size of a set means the amount of memory (in bytes) occupied by a set object. In this article, we will learn various ways to get th
2 min read
Multiple Sets Intersection in Python
In this article, a List of sets is given our task is to write a program to perform their intersection using Python. Examples of finding the intersection of multiple setsInput : test_list = [{5, 3, 6, 7}, {1, 3, 5, 2}, {7, 3, 8, 5}, {8, 4, 5, 3}] Output : {3, 5} Explanation : 3 and 5 is present in al
2 min read
Python - Union of Tuples
Sometimes, while working with tuples, we can have a problem in which we need union of two records. This type of application can come in Data Science domain. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using set() + "+" operator This task can be performed using union f
2 min read
Creating Sets of Tuples in Python
Tuples are an essential data structure in Python, providing a way to store ordered and immutable sequences of elements. When combined with sets, which are unordered collections of unique elements, you can create powerful and efficient data structures for various applications. In this article, we wil
3 min read
Make a Set of Lists in Python
Creating a set of lists typically involves defining multiple lists and organizing them into a structure, such as a dictionary or another list. In this article, we will see how we can make a set of lists in Python. Table of Content Creating a Set of List Using list() FunctionMake A Set Of Lists Using
3 min read
Python - Convert Matrix to Sets of Set
In this article, the task is to write Python program to Convert Matrix to sets of set. Examples: Input : test_list = [[5, 6, 3], [1, 7, 9], [8, 2, 0]]Output : {frozenset({8, 0, 2}), frozenset({1, 9, 7}), frozenset({3, 5, 6})}Explanation : Converted to sets of frozensets to remain immutable and hasha
5 min read
Python - Append Multiple elements in set
In Python, sets are an unordered and mutable collection of data type what does not contains any duplicate elements. In this article, we will learn how to append multiple elements in the set at once. Example: Input: test_set = {6, 4, 2, 7, 9}, up_ele = [1, 5, 10]Output: {1, 2, 4, 5, 6, 7, 9, 10}Expla
4 min read