Python | Find mismatch item on same index in two list
Last Updated :
09 May, 2023
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)
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)
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)
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
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".
Similar Reads
How to Find Index of Item in Python List To find the index of given list item in Python, we have multiple methods depending on specific use case. Whether weâre checking for membership, updating an item or extracting information, knowing how to get an index is fundamental. Using index() method is the simplest method to find index of list it
2 min read
Python - Find Index containing String in List In this article, we will explore how to find the index of a string in a list in Python. It involves identifying the position where a specific string appears within the list.Using index()index() method in Python is used to find the position of a specific string in a list. It returns the index of the
2 min read
Python Program to find tuple indices from other tuple list Given Tuples list and search list consisting of tuples to search, our task is to write a Python Program to extract indices of matching tuples. Input : test_list = [(4, 5), (7, 6), (1, 0), (3, 4)], search_tup = [(3, 4), (8, 9), (7, 6), (1, 2)]Output : [3, 1]Explanation : (3, 4) from search list is fo
8 min read
Python Program For Finding Intersection Point Of Two Linked Lists There are two singly linked lists in a system. By some programming error, the end node of one of the linked list got linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge. Above diagram shows an example with two linked lists havi
6 min read
Python program to find String in a List Searching for a string in a list is a common operation in Python. Whether we're dealing with small lists or large datasets, knowing how to efficiently search for strings can save both time and effort. In this article, weâll explore several methods to find a string in a list, starting from the most e
3 min read
Python - Filter unequal elements of two lists corresponding same index Sometimes, while working with Python data, we can have a problem in which we require to extract the values across multiple lists which are unequal and have similar index. This kind of problem can come in many domains. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using
5 min read
Python | Set Difference in list of dictionaries The difference of two lists have been discussed many times, but sometimes we have a large number of data and we need to find the difference i.e the elements in dict2 not in 1 to reduce the redundancies. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension The na
3 min read
Python program to remove duplicate elements index from other list Given two lists, the task is to write a Python program to remove all the index elements from 2nd list which are duplicate element indices from 1st list. Examples: Input : test_list1 = [3, 5, 6, 5, 3, 7, 8, 6], test_list2 = [1, 7, 6, 3, 7, 9, 10, 11] Output : [1, 7, 6, 9, 10] Explanation : 3, 7 and 1
7 min read
Python - Difference of Two Lists Including Duplicates We are given two list we need find difference between two list. For example, a = [1, 2, 2, 3, 4] and b = [2, 3] we need to find difference of two list so that resultant output should be [1, 2, 4].Using a for loopA for loop can iterate through first list and remove matching elements from second list
2 min read
Python | Check if two list of tuples are identical Sometimes, while working with tuples, we can have a problem in which we have list of tuples and we need to test if they are exactly identical. This is a very basic problem and can occur in any domain. Let's discuss certain ways in which this task can be done. Method #1 : Using == operator This is th
4 min read