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)



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 on a list of sets in Python: Using union() method i
4 min read
Python Program to Find Duplicate sets in list of sets
Given a list of sets, the task is to write a Python program to find duplicate sets. Input : test_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}]Output : [frozenset({1, 4, 5, 6}), frozenset({1, 3, 4})]Explanation : {1, 4, 5, 6} is similar to {6, 4, 1, 5} hence part of result. Input : test_list = [{4, 5, 6, 9}, {6, 4, 1, 5}, {
8 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 all the sets.Method 1: Using a Logical operator This
2 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}, {0, 3, 6, 7}] Output : {8, 1, 9, 0} Explanation :
3 min read
How to union multiple dataframe in PySpark?
In this article, we will discuss how to union multiple data frames in PySpark. Method 1: Union() function in pyspark The PySpark union() function is used to combine two or more data frames having the same structure or schema. This function returns an error if the schema of data frames differs from each other. Syntax: data_frame1.union(data_frame2)
4 min read
Python program to find common elements in three lists using sets
Prerequisite: Sets in Python Given three arrays, we have to find common elements in three sorted lists using sets. Examples : Input : ar1 = [1, 5, 10, 20, 40, 80] ar2 = [6, 7, 20, 80, 100] ar3 = [3, 4, 15, 20, 30, 70, 80, 120] Output : [80, 20] Input : ar1 = [1, 5, 5] ar2 = [3, 4, 5, 5, 10] ar3 = [5, 5, 10, 20] Output : [5] Method 1: We have given
5 min read
Find the union of two NumPy arrays
To find union of two 1-dimensional arrays we can use function numpy.union1d() of Python Numpy library. It returns unique, sorted array with values that are in either of the two input arrays. Syntax: numpy.union1d(array1, array2) Note The arrays given in input are flattened if they are not 1-dimensional. Let's see examples of how to find union of tw
2 min read
Python | Union of two or more Lists
Union of a list means, we must take all the elements from list A and list B (there can be more than two lists) and put them inside a single new list. There are various orders in which we can combine the lists. For e.g., we can maintain the repetition and order or remove the repeated elements in the final list and so on. Examples: Maintained repetit
7 min read
Python set operations (union, intersection, difference and symmetric difference)
This article demonstrates different operations on Python sets. Examples: Input : A = {0, 2, 4, 6, 8} B = {1, 2, 3, 4, 5} Output : Union : [0, 1, 2, 3, 4, 5, 6, 8] Intersection : [2, 4] Difference : [8, 0, 6] Symmetric difference : [0, 1, 3, 5, 6, 8] In Python, below quick operands can be used for different operations. | for union. & for interse
1 min read
Union() function in Python
Python set Union() Method returns a new set that contains all the items from the original set. The union of two given sets is the set that contains all the elements of both sets. The union of two given sets A and B is a set that consists of all the elements of A and all the elements of B such that no element is repeated. The symbol for denoting uni
3 min read
Practice Tags :
three90RightbarBannerImg