Open In App

Find the Union on List of Sets in Python

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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




# initializing list
list_of_sets = [{1, 2, 3}, {3, 4, 5}, {5, 6, 7}]
 
# printing original list
print("The original list is : " + str(list_of_sets))
 
# Initialize an empty set to store the union result
union_result = set()
 
# Iterate through each set in the list and find the union
for s in list_of_sets:
    union_result = union_result.union(s)
 
# printing result
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




# Initializing list
from functools import reduce
list_of_sets = [{1, 2, 3}, {3, 4, 5}, {5, 6, 7}]
 
# Printing original list
print("The original list is : " + str(list_of_sets))
 
# Import reduce function from functools module
# Find the union using functools.reduce and a lambda function
union_result = reduce(lambda x, y: x.union(y), list_of_sets)
 
# Printing result
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




# Initializing list
list_of_sets = [{1, 2, 3}, {3, 4, 5}, {5, 6, 7}]
 
# Printing original list
print("The original list is : " + str(list_of_sets))
 
# Initialize an empty set to store the union result
union_result = set()
 
# Iterate through each set in the list and update the union using update() method
for s in list_of_sets:
    union_result.update(s)
 
# Printing result
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




# Initializing list
from functools import reduce
list_of_sets = [{1, 2, 3}, {3, 4, 5}, {5, 6, 7}]
 
# Printing original list
print("The original list is : " + str(list_of_sets))
union_result = reduce(lambda x, y: x | y, list_of_sets)
 
# Printing result
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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads