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
# Python3 code to demonstrate working of # Append Missing elements from other List # Using list comprehension # initializing list test_list1 = [ 5 , 6 , 4 , 8 , 9 , 1 ] test_list2 = [ 9 , 8 , 7 ] # printing original lists print ( "The original list 1 is : " + str (test_list1)) print ( "The original list 2 is : " + str (test_list2)) # extracting elements from list 1 which are not in list 2 temp1 = [ele for ele in test_list1 if ele not in test_list2] # constructing result res = temp1 + test_list2 # printing result print ( "The modified list 2 : " + str (res)) |
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
# Python3 code to demonstrate working of # Append Missing elements from other List # Using set() + "-" operator + extend() # initializing list test_list1 = [ 5 , 6 , 4 , 8 , 9 , 1 ] test_list2 = [ 9 , 8 , 7 ] # printing original lists print ( "The original list 1 is : " + str (test_list1)) print ( "The original list 2 is : " + str (test_list2)) # finding missing words rem_list = ( set (test_list1) - set (test_list2)) # checking order res = [ele for ele in test_list1 if ele in rem_list] # joining result res.extend(test_list2) # printing result print ( "The modified list 2 : " + str (res)) |
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
# Python3 code to demonstrate working of # Append Missing elements from other List from collections import Counter # initializing list test_list1 = [ 5 , 6 , 4 , 8 , 9 , 1 ] test_list2 = [ 9 , 8 , 7 ] # printing original lists 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) # printing result print ( "The modified list 2 : " + str (res)) |
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
# Python3 code to demonstrate working of # Append Missing elements from other List import operator as op # initializing list test_list1 = [ 5 , 6 , 4 , 8 , 9 , 1 ] test_list2 = [ 9 , 8 , 7 ] # printing original lists print ( "The original list 1 is : " + str (test_list1)) print ( "The original list 2 is : " + str (test_list2)) # extracting elements from list 1 which are not in list 2 temp1 = [ele for ele in test_list1 if op.countOf(test_list2,ele) = = 0 ] # constructing result res = temp1 + test_list2 # printing result print ( "The modified list 2 : " + str (res)) |
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) # initializing list test_list1 = [ 5 , 6 , 4 , 8 , 9 , 1 ] test_list2 = [ 9 , 8 , 7 ] # printing original lists print ( "The original list 1 is : " + str (test_list1)) print ( "The original list 2 is : " + str (test_list2)) # result list result = append_missing(test_list1, test_list2) # printing result print ( "The modified list 2 : " + str (result)) #this code contributed by tvsk |
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 # initializing lists test_list1 = [ 5 , 6 , 4 , 8 , 9 , 1 ] test_list2 = [ 9 , 8 , 7 ] # printing original lists 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) # printing result print ( "The modified list 2 : " + str (res.tolist())) #This code is contributed by Rayudu. |
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
# Python3 code to demonstrate working of # Append Missing elements from other List # Using itertools.filterfalse() # import the itertools module import itertools # initializing list test_list1 = [ 5 , 6 , 4 , 8 , 9 , 1 ] test_list2 = [ 9 , 8 , 7 ] # printing original lists print ( "The original list 1 is : " + str (test_list1)) print ( "The original list 2 is : " + str (test_list2)) # extracting elements from list 1 which are not in list 2 temp1 = list (itertools.filterfalse( lambda ele: ele in test_list2, test_list1)) # constructing result res = temp1 + test_list2 # printing result print ( "The modified list 2 : " + str (res)) |
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)