Open In App

Python – Find union of multiple sets

Last Updated : 01 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Union multiple sets
# Using union() + * operator
 
# initializing list
test_list = [{4, 3, 5, 2}, {8, 4, 7, 2}, {1, 2, 3, 4}, {9, 5, 3, 7}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# * operator packs sets for union
res = set().union(*test_list)
 
# printing result
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




# Python3 code to demonstrate working of
# Union multiple sets
# Using chain.from_iterable() + * operator
from itertools import chain
 
# initializing list
test_list = [{4, 3, 5, 2}, {8, 4, 7, 2}, {1, 2, 3, 4}, {9, 5, 3, 7}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# * operator packs sets for union
res = set(chain(*test_list))
 
# printing result
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




# Python3 code to demonstrate working of
# Union multiple sets
from collections import Counter
from itertools import chain
 
# initializing list
test_list = [{4, 3, 5, 2}, {8, 4, 7, 2}, {1, 2, 3, 4}, {9, 5, 3, 7}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# * operator packs sets for union
res = list(Counter(chain(*test_list)).keys())
res.sort()
 
# printing result
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




# Python3 code to demonstrate working of
# Union multiple sets
 
# initializing list
test_list = [{4, 3, 5, 2}, {8, 4, 7, 2}, {1, 2, 3, 4}, {9, 5, 3, 7}]
 
# printing original list
print("The original list is : " + str(test_list))
 
 
res=set()
for i in test_list:
    res.update(i)
# printing result
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




# Using reduce() function from functools module
from functools import reduce
# Initializing list of sets
test_list = [{4, 3, 5, 2}, {8, 4, 7, 2}, {1, 2, 3, 4}, {9, 5, 3, 7}]
# Printing original list of sets
print("The original list is : " + str(test_list))
# Using reduce() function to obtain union of multiple sets
res = reduce(set.union, test_list)
# Printing result
print("Multiple set union  : " + str(res))
#This is contributed by Jyothi pinjala


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




# initialize the list of sets
test_list = [{4, 3, 5, 2}, {8, 4, 7, 2}, {1, 2, 3, 4}, {9, 5, 3, 7}]
# Printing original list of sets
print("The original list is : " + str(test_list))
# create an empty set to store the union
result = set()
 
# use set.update() method to find the union
for s in test_list:
    result.update(s)
 
# print the result
print("The union of multiple sets:", result)
#This code is contributed by Vinay Pinjala


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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads