Python | Union of two or more lists

Python Methods and Functions

List concatenation means that we have to take all the elements from list A and list B (there can be more than two lists) and put them in one new list. There are various orders in which we can combine lists. For example, we can maintain repetition and order, or remove duplicate items in the final list, and so on. 
Examples:

  Maintained repetition only  Input: lst1 = [23, 15, 2, 14, 14, 16, 20, 52] lst2 = [2, 48 , 15, 12, 26, 32, 47, 54] Output: [23, 15, 2, 14, 14, 16, 20, 52, 2, 48, 15, 12, 26, 32, 47, 54]  Maintained repetition and order  Input: lst1 = [23, 15, 2, 14, 14, 16, 20, 52] lst2 = [2, 48, 15, 12, 26, 32, 47, 54] Output : [2, 2, 12, 14, 14, 15, 15, 16, 20, 23, 26, 32, 47, 48, 52, 54]  Without repetition  Input: lst1 = [23, 15, 2, 14, 14, 16, 20, 52] lst2 = [2, 48, 15, 12, 26, 32, 47, 54] Output: [32, 2, 12, 14, 15, 16, 48, 47, 20, 52, 54, 23, 26]  Union of three lists  Input: lst1 = [23, 15, 2, 14, 14, 16, 20, 52] lst2 = [2, 48 , 15, 12, 26, 32, 47, 54] lst3 = [4, 78, 5, 6, 9, 25, 64, 32, 59] Output: [32, 64, 2, 4, 5, 6, 9 , 12, 14, 15, 16, 48, 47, 78, 20, 52, 54, 23, 25, 26, 59] 

Maintain repetition

We can just use the + operator,  to combine the two lists into one. This will reflect repetition.

# Python program to illustrate merging
# Supported repetition

def Union (lst1, lst2):

final_list = lst1 + lst2

return final_list

 
Driver code

lst1 = [ 23 , 15 , 2 , 14 , 14 , 16 , 20 , 52 ]

lst2 = [ 2 , 48 , 15 , 12 , 26 , 32 , 47 , 54 ]

print (Union (lst1, lst2))

Output:

 [23, 15, 2, 14, 14, 16, 20, 52, 2, 48, 15, 12, 26, 32, 47, 54] 

Maintaining both repetition and order

To maintain the order of appearing in the new list, we need to use the sorted () function , passing the addition of two lists as parameters (plus with an operation, as in the previous task).

# Python program to illustrate merging
# Repetition and order supported

def Union (lst1, lst2):

  final_list = sorted (lst1 + lst2)

return final_list

 
Driver code

lst1 = [ 23 , 15 , 2 , 14 , 14 , 16 , 20 , 52 ]

lst2 = [ 2 , 48 , 15 , 12 , 26 , 32 , 47 , 54 ]

print (Union (lst1, lst2))

Output:

 [2, 2, 12, 14, 14, 15, 15, 16, 20, 23, 26, 32, 47, 48, 52, 54] 

No repetition

To get rid of all duplicate items from the initial list, we use the set () function on both lists separately. We then add them using the "+" operator and pass them as a new list.

# Python program to illustrate the merge
# No repetition

def Union (lst1, lst2):

final_list = list ( set (lst1) | set (lst2))

return final_list

 
Driver code

lst1  = [ 23 , 15 , 2 , 14 , 14 , 16 , 20 , 52 ]

lst2 = [ 2 , 48 , 15 , 12 , 26 , 32 , 47 , 54 ]

print (Union (lst1, lst2))

Output:

 [32, 2, 12, 14, 15, 16, 48, 47, 20, 52, 54, 23, 26] 

More than two lists

We can also combine more than two lists. This can be done efficiently by using both set () and union () functions, as shown in the example below. It also takes care of and prevents repetition.

# Python program to illustrate the merge
# Union of three lists

def Union (lst1, lst2, lst3):

final_list = list ( set (). union (lst1, lst2, lst3))

return final_list

 
Driver code

lst1 = [ 23 , 15 , 2 , 14 , 14 , 16 , 20 , 52 ]

lst2 = [ 2 , 48 , 15 , 12 , 26 , 32 , 47 , 54 ]

lst3 = [ 4 , 78 , 5 , 6 , 9 , 25 , 64 , 32 , 59 ]

print (Union (lst1, lst2, lst3))

Exit:

 [32, 64, 2, 4, 5, 6, 9, 12, 14, 15, 16, 48, 47, 78, 20, 52, 54, 23, 25, 26, 59]