Python Set Union() of list of sets

Python Set

In this post, we are going to understand how to use Python Set Union() method with the examples, how to get Python Set Union() of the list of sets. Python language supports the Mathematical operations on sets.

Union is a mathematical term that means to merge the elements of the sets, but in this merged copy we get only distinct elements.So for example, if two sets have a common element then in the Union of these sets that common element will show up as one time only.

Let us understand this with the help of examples below for the union of two sets and union of multiple sets.

What is Python Set Union


Python set data structure provides us Union() methods that help us to perform the Union Operation on sets. Set Union() method returns a new set of distinct elements from all Sets. We can be achieved the union of sets using the Union() method or | operator.

Syntax Union()

#Synatx of Union Method

set.union(*others)

Parameters

  • *Others: It can be a set of iterables (tuple, list, dictionary, string).
  • *(asterisk): it represents multiple arguments that we can pass to Union() method.

Return value

It Returns a new set of all distinct elements from a single or multiple sets or iterables.

1. Python find union of Mutiple Set


In this example we have three sets (fruit_Set,animal_set,num_set).We are finding the union of these SET using the Union() method to pass all as parameters to the union() method. Printing the result using the print() method.

Program to find Python Mutiple Set Union

fruit_Set = {'apple','kiwi','Fig'}

animal_set = {'Dog','Cat','Bunny'}

num_set = {1,2}

#Union of multiple sets
result_set = fruit_Set.union(animal_set,num_set)

print('Mutiple sets union: \n',result_set )

Output

Mutiple sets union: 
 {'Cat', 'Fig', 1, 2, 'kiwi', 'apple', 'Bunny', 'Dog'}

2. Find Mutiple Python Sets Using| operator


In this code example, we are using the | operator to find the union of multiple sets. We can’t pass iterable as an argument to | operator.

fruit_Set = {'apple','kiwi','Fig'}
animal_set = {'Dog','Cat','Bunny'}
num_set = {1,2}

#Union of two sets using | operator

print('union sets using | operator :\n ',fruit_Set|animal_set|num_set)

Output

union sets using | operator :
  {'Fig', 'Cat', 'Bunny', 'apple', 'kiwi', 'Dog', 1, 2}

3.Python find Union of a list of Sets


We have a list of the set (list_of_Sets) we are passing this list to union() method as a parameter and Finally converting into SET.

Printing the output result using the print() method.

Python Program to Union of a list of Sets

list_of_Sets = [{1, 'at', 3}, {12, 24}, {20, 30, 50},{12,16}
]

# One line code to union a list of sets
union_of_sets = set().union(*list_of_Sets)



print('union of multiple set:\n ',union_of_sets )

Output

union of multiple set:
  {1, 3, 'at', 12, 16, 50, 20, 24, 30}

As we know Python set union() method can take an iterable as an argument, So In this example, we are joining distinct elements of multiple iterables those includes (tuple, list, Dictionary, String) using union.

4. Python find Union of multiple List


The Python Set union method can be taken multiple iterables as an argument.

In this example, we are Passing multiple lists to the Set union() method to find the union.

Python Program to find Union of multiple List

Num_set = {12,16}

# define a list
animal_list = ['Dog','Cat']

#list 
fruit_list = ['apple','bana','kiwi']

Union_list = Num_set.union(animal_list,fruit_list )

print('union of multiple iterables :\n ',Union_list )

Output

union of multiple list :
  {'apple', 'Dog', 'kiwi', 12, 16, 'bana', 'Cat'}

5. Python Set Union of multiple dictionaries


In this are finding the union of multiple dictionaries using the Python Set union() method.

Note: While finding the union of multiple dictionaries only keys of dictionary are added to set not values.

Program to get Union of multiple dictionaries

Num_set = {12,16}

fruits_dict = {'App':100,'Bana':70,'orge':56}


#define a dictionary
dict = {'A':65,'B':66}

#Union multiple iterables

Union_dict = Num_set.union(fruits_dict,dict)

print('union of multiple dict:\n ',Union_dict )

Output

union of multiple dict:
  {16, 'App', 'A', 'B', 'Bana', 12, 'orge'}

Conclusion

In this post, we have explored how to use Python Set Union() method with examples that include union multiple sets using the unions() method and | operator. Union list of sets in one line, union of multiple lists, and dictionaries.