Python Set Union with Example – Python Tutorial

6 years ago Lalit Bhagtani 0

In this tutorial, we will learn about different ways of performing union operation on two or more sets in python.
Python Set Union

Union of Sets:

In set theory, the union of two or more sets is the set which contains all the elements ( distinct ) present in all the sets. For example :

A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8, 9}

Union of A & B :-
A U B = {1, 2, 3, 4, 5, 6, 7, 8, 9}

Python Set Union

Python Set Union :

In Python, there are four different ways of performing union operation on collection of sets, they are as follows :

  1. union Method
  2. | Operator
  3. update Method
  4. |= Operator

union Method :

This method is used to return the union of a set and the set of elements from one or more iterable like string, list, set. This method takes arbitrary number of iterable objects as argument, which means one or more iterable can be passed. If iterable other than set is passed, it first converts the iterable object to set object and then perform the union operation. It returns a newly created set, which contains all the elements ( distinct ) present in all the iterables.

Syntax : < Set Object >.union( *<iterable object> ) : < Set Object >

Example:

# Python Set Union # create set object and assign it to variable A A = {1,2,3,4,5} # create set object and assign it to variable B B = {4,5,6,7,8,9} # call union method to get union of set A and B and assign it to variable S S = A.union(B) # print all the values of set A, B and S print('A : ',A) print('B : ',B) print('A Union B : ',S) # create list object and assign it to variable L L = [11,12,13,14,15] # call union method to get union of set A, B and list L and assign it to variable S S = A.union(B,L) # print all the values of set A, B, S and list L print('A : ',A) print('B : ',B) print('L : ',L) print('A Union B Union L : ',S)

| Operator :

This operator is used to return the union of two or more sets just like union() method. The difference between | operator and union() method is that, the former can work only with set objects while latter can work with any iterable objects like list, string, set.

Syntax : < Set Object 1 > |  < Set Object 2 > | < Set Object 2 >… : < Set Object >

Example:

# Python Set Union # create set object and assign it to variable A A = {1,2,3,4,5} # create set object and assign it to variable B B = {4,5,6,7,8,9} # use | operator to get union of set A and B and assign it to variable S S = A | B # print all the values of set A, B and S print('A : ',A) print('B : ',B) print('A Union B : ',S) # create set object and assign it to variable C C = {7,8,9,10,11,12} # use | operator to get union of set A and B and C and assign it to variable S S = A | B | C # print all the values of set A, B, C and S print('A : ',A) print('B : ',B) print('C : ',C) print('A Union B Union C : ',S) # create list object and assign it to variable L L = [11,12,13,14,15] # use | operator to get union of set A and list L S = A | L # TypeError is raised as | opearator works with set operands only

update Method :

This method is used to return the union of a set and the set of elements from one or more iterable like string, list, set. It is very similar to union() method, with difference is that where union() method create and return a new set, containing all the elements ( distinct ) present in all the iterables, update() method updates the set on which this method is called with all the distinct elements present in all the iterables.

Syntax : < Set Object >.update( *<iterable object> ) 

Example:

# Python Set Union # create set object and assign it to variable A A = {1,2,3,4,5} # create set object and assign it to variable B B = {4,5,6,7,8,9} # call update method to get union of set A and B by updating set A A.update(B) # print all the values of set A print('A Union B : ',A) # create list object and assign it to variable L L = [11,12,13,14,15] # call update method to get union of set A, B and list L by updating set A A.update(B,L) # print all the values of set A print('A Union B Union L : ',A)

|= Operator :

This operator is used to return the union of two or more sets just like update() method. The difference between |= operator and update() method is that, the former can work only with set objects while latter can work with any iterable objects like list, string, set.

Syntax : < Set Object 1 > |=  < Set Object 2 > | < Set Object 2 >… 

Example:

# Python Set Union # create set object and assign it to variable A A = {1,2,3,4,5} # create set object and assign it to variable B B = {4,5,6,7,8,9} # use |= operator to get union of set A and B by updating set A A |= B # print all the values of set A print('A Union B : ',A) # create set object and assign it to variable A A = {1,2,3,4,5} # create set object and assign it to variable C C = {7,8,9,10,11,12} # use |= operator to get union of set A, B and C by updating set A A |= B | C # print all the values of set A print('A Union B Union C : ',A) # create list object and assign it to variable L L = [11,12,13,14,15] # use |= operator to get union of set A and list L A |= L # TypeError is raised as |= opearator works with set operands only

References :-

  1. union method Docs
  2. update method Docs

That’s all for Python Set Union with Example. If you liked it, please share your thoughts in comments section and share it with others too.

Previous