Find the Union on List of Sets in Python
Last Updated :
08 Feb, 2024
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 Python
Below are some of the ways by which we can find the union on a list of sets in Python:
- Using
union()
method iteratively
- Using
functools.reduce
- Using
set()
constructor and update()
method
- Using
|
operator with functools.reduce
Find the Union on a List of Sets Using union()
method
In this approach, we initialize an empty set (union_result
) and iterate through each set in the list (list_of_sets
). For each set, we use the union()
method to combine its elements with the existing union_result
. This process is repeated for all sets in the list, resulting in the union of all sets.
Python3
list_of_sets = [{ 1 , 2 , 3 }, { 3 , 4 , 5 }, { 5 , 6 , 7 }]
print ( "The original list is : " + str (list_of_sets))
union_result = set ()
for s in list_of_sets:
union_result = union_result.union(s)
print ( "Union of sets:" , union_result)
|
Output
The original list is : [{1, 2, 3}, {3, 4, 5}, {5, 6, 7}]
Union of sets: {1, 2, 3, 4, 5, 6, 7}
Finding the Union on a List of Sets Using functools.reduce()
In this approach, we use the reduce()
function from the functools
module along with a lambda function. The lambda function takes two sets and finds their union. The reduce()
function applies this lambda function cumulatively to all sets in the list, resulting in the overall union of sets.
Python3
from functools import reduce
list_of_sets = [{ 1 , 2 , 3 }, { 3 , 4 , 5 }, { 5 , 6 , 7 }]
print ( "The original list is : " + str (list_of_sets))
union_result = reduce ( lambda x, y: x.union(y), list_of_sets)
print ( "Union of sets:" , union_result)
|
Output
The original list is : [{1, 2, 3}, {3, 4, 5}, {5, 6, 7}]
Union of sets: {1, 2, 3, 4, 5, 6, 7}
Finding the Union on a List of Sets Using set()
and update()
method
In this approach we initialize an empty set (union_result
) and iterate through each set in the list. Here Instead of using the union()
method, we use the update()
method, which modifies the set in place by adding elements from the current set. This way, the union_result
is updated with the elements of each set in the list.
Python3
list_of_sets = [{ 1 , 2 , 3 }, { 3 , 4 , 5 }, { 5 , 6 , 7 }]
print ( "The original list is : " + str (list_of_sets))
union_result = set ()
for s in list_of_sets:
union_result.update(s)
print ( "Union of sets " , union_result)
|
Output
The original list is : [{1, 2, 3}, {3, 4, 5}, {5, 6, 7}]
Union of sets {1, 2, 3, 4, 5, 6, 7}
Finding the Union on a List of Sets Using |
operator
In this approach we use the reduce()
function along with a lambda function.Also we use the |
operator, which is an alternative way of expressing the union operation. The lambda function applies the |
operator cumulatively to all sets in the list.
Python3
from functools import reduce
list_of_sets = [{ 1 , 2 , 3 }, { 3 , 4 , 5 }, { 5 , 6 , 7 }]
print ( "The original list is : " + str (list_of_sets))
union_result = reduce ( lambda x, y: x | y, list_of_sets)
print ( "Union of sets :" , union_result)
|
Output
The original list is : [{1, 2, 3}, {3, 4, 5}, {5, 6, 7}]
Union of sets : {1, 2, 3, 4, 5, 6, 7}
Conclusion
In this article, we studied about deifferent approaches to finding the union of sets within a list in Python. Each approach showcased a different technique, providing flexibility and catering to various coding preferences. Whether employing the union() method iteratively, utilizing functools.reduce, leveraging the set() constructor with the update() method, using the | operator with functools.reduce, or utilizing list comprehension with the union() method, Python developers have multiple options for handling scenarios where the union of sets is a crucial aspect of data manipulation.
Share your thoughts in the comments
Please Login to comment...