Python | Find mismatch item on same index in two list
Given two list of integers, the task is to find the index at which the element of two list doesn’t match.
Input: Input1 = [1, 2, 3, 4] Input2 = [1, 5, 3, 6] Output: [1, 3] Explanation: At index=1 we have 2 and 5 and at index=3 we have 4 and 6 which mismatches.
Below are some ways to achieve this task.
Method #1: Using Iteration
Python3
# Python code to find the index at which the # element of two list doesn't match. # List initialisation Input1 = [ 1 , 2 , 3 , 4 ] Input2 = [ 1 , 5 , 3 , 6 ] # Index initialisation y = 0 # Output list initialisation Output = [] # Using iteration to find for x in Input1: if x ! = Input2[y]: Output.append(y) y = y + 1 # Printing output print (Output) |
[1, 3]
Time complexity: O(n), where n is the length of the lists Input1 and Input2, because the code iterates through the elements of the lists once.
Auxiliary space: O(m), where m is the length of the Output list, because the code creates an Output list to store the indexes at which the elements of the two lists do not match.
Method #2: Using list Comprehension and zip
Python3
# Python code to find the index at which the # element of two list doesn't match. # List initialisation Input1 = [ 1 , 2 , 3 , 4 ] Input2 = [ 1 , 5 , 3 , 6 ] # Using list comprehension and zip Output = [Input2.index(y) for x, y in zip (Input1, Input2) if y ! = x] # Printing output print (Output) |
[1, 3]
Time Complexity: O(n*n) where n is the number of elements in the dictionary. The list comprehension and zip is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(1) constant additional space is required
Method #3: Using Enumerate
Python3
# Python code to find the index at which the # element of two list doesn't match. # List initialisation Input1 = [ 1 , 2 , 3 , 4 ] Input2 = [ 1 , 5 , 3 , 6 ] # Using list comprehension and enumerate Output = [index for index, elem in enumerate (Input2) if elem ! = Input1[index]] # Printing output print (Output) |
[1, 3]
Method #4: Using filter, lambda
Here is an example using the filter function and a lambda function to find the index at which the element of two lists don’t match:
Python3
Input1 = [ 1 , 2 , 3 , 4 ] Input2 = [ 1 , 5 , 3 , 6 ] # Using filter and lambda Output = list ( filter ( lambda x: Input1[x[ 0 ]] ! = x[ 1 ], enumerate (Input2))) # Output is a list of tuples, so we need to extract just the index Output = [x[ 0 ] for x in Output] print (Output) #This code is contributed by Edula Vinay Kumar Reddy |
[1, 3]
This approach first enumerates the Input2 list, so that we have a list of tuples with the format (index, element). Then, the filter function is used to select only the tuples where the element of Input2 at that index does not match the corresponding element of Input1. Finally, we extract just the indices from the tuples using a list comprehension.
Time complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using numpy
Another approach to finding the index at which the elements of two lists don’t match is to use the numpy library. This approach converts the lists to numpy arrays and uses numpy’s built-in functions to perform the comparison.
Python3
#Importing numpy library import numpy as np #List initialisation Input1 = [ 1 , 2 , 3 , 4 ] Input2 = [ 1 , 5 , 3 , 6 ] #Convert lists to numpy arrays arr1 = np.array(Input1) arr2 = np.array(Input2) #Using numpy's built-in functions to find the mismatch Output = np.where(arr1 ! = arr2)[ 0 ] #Printing output print (Output) |
Output:
[1 3]
Time complexity: O(n), where n is the length of the lists Input1 and Input2, because the code performs element-wise comparison on the numpy arrays.
Auxiliary space: O(n), where n is the length of the Output array, because the code creates an Output array to store the indexes at which the elements of the two lists do not match.
Note: It’s important to note that this approach requires the numpy library to be installed. If numpy is not already installed, it can be installed using pip: “pip install numpy”.