Python | Union of two or more Lists
Union of a list means, we must take all the elements from list A and list B (there can be more than two lists) and put them inside a single new list. There are various orders in which we can combine the lists. For e.g., we can maintain the repetition and order or remove the repeated elements 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]
Maintaining Repetition
We can simply use the plus “+” operator inorder to combine two list into one. This will reflect the repetition.
Python3
# Python program to illustrate union # Maintained 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]
Time complexity: O(n), where n is the length of the longest list.
Auxiliary space complexity: O(n), where n is the total number of elements in both lists.
Maintaining both Repetition and Order
To maintain the order of appearance in the new list we need to use the sorted() function, passing the addition of two lists(plus operated, as in the previous problem) as parameters.
Python3
# Python program to illustrate union # Maintained repetition and order 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]
The time complexity of the Union function is O(n log n), where n is the total number of elements in both lists.
The space complexity of the Union function is O(n), where n is the total number of elements in both lists.
Without Repetition
To get rid of all the repetitive elements from the initial list, we use the set() function on both the lists, individually. Then we add them using the “+” operator and pass as a new list.
Python3
# Python program to illustrate union # Without 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 make an union of more than two lists. This can be done efficiently by using both the set() and union() function, simultaneously, as shown in the below example. This also takes care of the repetition and prevents them.
Python3
# Python program to illustrate union # 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)) |
Output:
[32, 64, 2, 4, 5, 6, 9, 12, 14, 15, 16, 48, 47, 78, 20, 52, 54, 23, 25, 26, 59]
Approach : Using extend() method
Step-by-step approach:
- First, import the “collections” module in the Python program.
- Define two lists: lst1 and lst2 which contain some integer values.
- Use the extend() method to append all the elements of lst2 to lst1. Now, lst1 contains all the elements of both lst1 and lst2.
- Use the print() function to display the combined list, lst1, which maintains the repetition of elements.
- Use the set() function to create a set from lst1, which will not maintain the repetition of elements or the order of elements.
- Use the list() function to convert the resulting set back into a list.
- Use the print() function to display the new list, x, which does not maintain repetition or order.
Below is the implementation of the above approach:
Python3
# Python program to illustrate union lst1 = [ 23 , 15 , 2 , 14 , 14 , 16 , 20 , 52 ] lst2 = [ 2 , 48 , 15 , 12 , 26 , 32 , 47 , 54 ] lst1.extend(lst2) # Maintaining repetition print ( "Maintaining repetition " + str (lst1)) # Not maintaining repetition and order x = list ( set (lst1)) print ( "Not maintaining repetition " + str (x)) |
Maintaining repetition [23, 15, 2, 14, 14, 16, 20, 52, 2, 48, 15, 12, 26, 32, 47, 54] Not maintaining repetition [32, 2, 12, 14, 15, 16, 48, 47, 20, 52, 54, 23, 26]
Please Login to comment...