![]() |
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:
Find the Union on a List of Sets Using u
|
# 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) |
The original list is : [{1, 2, 3}, {3, 4, 5}, {5, 6, 7}] Union of sets: {1, 2, 3, 4, 5, 6, 7}
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.
# 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) |
The original list is : [{1, 2, 3}, {3, 4, 5}, {5, 6, 7}] Union of sets: {1, 2, 3, 4, 5, 6, 7}
set()
and update()
methodIn 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.
# 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) |
The original list is : [{1, 2, 3}, {3, 4, 5}, {5, 6, 7}] Union of sets {1, 2, 3, 4, 5, 6, 7}
|
operatorIn 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.
# 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) |
The original list is : [{1, 2, 3}, {3, 4, 5}, {5, 6, 7}] Union of sets : {1, 2, 3, 4, 5, 6, 7}
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.
Reffered: https://www.geeksforgeeks.org
Python |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |