Given 2 lists, append elements missing from list 1 to list 2.
Input : test_list1 = [5, 6, 4, 8, 9, 1], test_list2 = [9, 8, 10]
Output : [5, 6, 4, 1, 9, 8, 10]
Explanation : 5, 6, 4, 1 added to list 2, in order.
Input : test_list1 = [5, 6, 4, 8, 9, 1], test_list2 = [9, 10]
Output : [5, 6, 4, 8, 1, 9, 10]
Explanation : 5, 6, 4, 8, 1 added to list 2, in order.
Method #1 : Using list comprehension
In this, we iterate list 1 to check for missing elements in list 2, then add these elements to list 2.
Python3
test_list1 = [ 5 , 6 , 4 , 8 , 9 , 1 ]
test_list2 = [ 9 , 8 , 7 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
temp1 = [ele for ele in test_list1 if ele not in test_list2]
res = temp1 + test_list2
print ( "The modified list 2 : " + str (res))
|
Output
The original list 1 is : [5, 6, 4, 8, 9, 1]
The original list 2 is : [9, 8, 7]
The modified list 2 : [5, 6, 4, 1, 9, 8, 7]
Time complexity of this approach is O(n). It requires one traversal of both the lists.
Auxiliary space: O(n). We need an extra list to store the elements of list 1 which are not present in list 2.
Method #2: Using set() + “-” operator + extend()
In this, we check for elements of list 1 missing from list 2 using set() and – operator and extend() is used to join both the list to get desired result.
Python3
test_list1 = [ 5 , 6 , 4 , 8 , 9 , 1 ]
test_list2 = [ 9 , 8 , 7 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
rem_list = ( set (test_list1) - set (test_list2))
res = [ele for ele in test_list1 if ele in rem_list]
res.extend(test_list2)
print ( "The modified list 2 : " + str (res))
|
Output
The original list 1 is : [5, 6, 4, 8, 9, 1]
The original list 2 is : [9, 8, 7]
The modified list 2 : [5, 6, 4, 1, 9, 8, 7]
Time complexity: O(n*n), where n is the length of the test_list. The set() + “-” operator + extend() takes O(n*n) time.
Auxiliary Space: O(n), extra space of size n is required.
Method #3: Using Counter() function
Python3
from collections import Counter
test_list1 = [ 5 , 6 , 4 , 8 , 9 , 1 ]
test_list2 = [ 9 , 8 , 7 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
freq = Counter(test_list2)
res = []
for i in test_list1:
if i not in freq.keys():
res.append(i)
res.extend(test_list2)
print ( "The modified list 2 : " + str (res))
|
Output
The original list 1 is : [5, 6, 4, 8, 9, 1]
The original list 2 is : [9, 8, 7]
The modified list 2 : [5, 6, 4, 1, 9, 8, 7]
Time Complexity:O(n)
Auxiliary Space: O(n)
Method #4: Using Operator.countOf() method
Python3
import operator as op
test_list1 = [ 5 , 6 , 4 , 8 , 9 , 1 ]
test_list2 = [ 9 , 8 , 7 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
temp1 = [ele for ele in test_list1 if op.countOf(test_list2,ele) = = 0 ]
res = temp1 + test_list2
print ( "The modified list 2 : " + str (res))
|
Output
The original list 1 is : [5, 6, 4, 8, 9, 1]
The original list 2 is : [9, 8, 7]
The modified list 2 : [5, 6, 4, 1, 9, 8, 7]
Time Complexity:O(N)
Auxiliary Space: O(N)
Method#5: Using Recursive method
Python3
def append_missing(l1, l2):
if not l1:
return l2
if l1[ 0 ] in l2:
return append_missing(l1[ 1 :], l2)
else :
return [l1[ 0 ]] + append_missing(l1[ 1 :], l2)
test_list1 = [ 5 , 6 , 4 , 8 , 9 , 1 ]
test_list2 = [ 9 , 8 , 7 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
result = append_missing(test_list1, test_list2)
print ( "The modified list 2 : " + str (result))
|
Output
The original list 1 is : [5, 6, 4, 8, 9, 1]
The original list 2 is : [9, 8, 7]
The modified list 2 : [5, 6, 4, 1, 9, 8, 7]
Time Complexity :O(N)
Auxiliary Space: O(N)
Method#6: Using numpy:
- Initialize two lists test_list1 and test_list2.
- Print the original lists test_list1 and test_list2.
- Call the numpy union1d() function with test_list1 and test_list2 as arguments.
- Store the result in a variable named res.
- Print the modified list 2, which is the union of test_list1 and test_list2.
Python3
import numpy as np
test_list1 = [ 5 , 6 , 4 , 8 , 9 , 1 ]
test_list2 = [ 9 , 8 , 7 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = np.union1d(test_list1, test_list2)
print ( "The modified list 2 : " + str (res.tolist()))
|
Output:
The original list 1 is : [5, 6, 4, 8, 9, 1]
The original list 2 is : [9, 8, 7]
The modified list 2 : [1, 4, 5, 6, 7, 8, 9]
Time complexity: O(m + n), where m is the size of test_list1 and n is the size of test_list2. This is because the numpy union1d() function has to go through both lists to find the union.
Auxiliary Space: O(k), where k is the size of the resulting list. This is because the numpy union1d() function creates a new array to store the result.
Method #7: Using itertools.filterfalse() method
Steps:
- Import the itertools module.
- Use the filterfalse() method to filter out the elements from test_list1 that are present in test_list2.
- Convert the output of the filterfalse() method into a list and store it in a variable named temp1.
- Concatenate temp1 and test_list2 and store the result in a variable named res.
- Print the modified list res.
Python3
import itertools
test_list1 = [ 5 , 6 , 4 , 8 , 9 , 1 ]
test_list2 = [ 9 , 8 , 7 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
temp1 = list (itertools.filterfalse( lambda ele: ele in test_list2, test_list1))
res = temp1 + test_list2
print ( "The modified list 2 : " + str (res))
|
Output
The original list 1 is : [5, 6, 4, 8, 9, 1]
The original list 2 is : [9, 8, 7]
The modified list 2 : [5, 6, 4, 1, 9, 8, 7]
Time complexity: O(n)
Auxiliary space: O(n)
Share your thoughts in the comments
Please Login to comment...