
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Find mismatch item on same index in two list in Python
Sometimes we may need to compare elements in two python lists in terms of both their value and position or index. In this article we will see how to find out the elements in two lists at the same position which do not match in their value.
Using for loop
we can design for loop to compare the values at similar indexes. Id the values do not match then we add the index to a result list. The for loop first fetches the value in the first index and then uses an if condition to compare it with values from the second list.
Example
listA= [13, 'Mon',23, 62,'Sun'] listB = [5, 'Mon',23, 6,'Sun'] # index variable idx = 0 # Result list res = [] # With iteration for i in listA: if i != listB[idx]: res.append(idx) idx = idx + 1 # Result print("The index positions with mismatched values:\n",res)
Output
Running the above code gives us the following result −
The index positions with mismatched values: [0, 3]
With zip
The zip function helps us to write a shorter code as we compare the elements from each index. The index values are captured for positions where the element’s value do not match.
Example
listA= [13, 'Mon',23, 62,'Sun'] listB = [5, 'Mon',23, 6,'Sun'] res = [listB.index(n) for m, n in zip(listA, listB) if n != m] # Result print("The index positions with mismatched values:\n",res)
Output
Running the above code gives us the following result −
The index positions with mismatched values: [0, 3]
With enumerate
It is similar to the approach in zip function except that here we have for loop to go through each element and index when applying enumerate function to one of the list.
Example
listA= [13, 'Mon',23, 62,'Sun'] listB = [5, 'Mon',23, 6,'Sun'] res = [idx for idx, elem in enumerate(listB) if elem != listA[idx]] # Result print("The index positions with mismatched values:\n",res)
Output
Running the above code gives us the following result −
The index positions with mismatched values: [0, 3]
- Related Articles
- How to find the index of a list item in Swift?
- How to find the index of an item given a list containing it in Python?
- Equate two list index elements in Python
- How to find the index of an item in a C# list in a single step?
- Add list elements with a multi-list based on index in Python
- Program to find duplicate item from a list of elements in Python
- Find minimum of each index in list of lists in Python
- Find same contacts in a list of contacts in Python
- Python - Inserting item in sorted list maintaining order
- Python – Index Value repetition in List
- Program to find smallest index for which array element is also same as index in Python
- Python – Elements with same index
- Python Index specific cyclic iteration in list
- Python – Negative index of Element in List
- Set Mismatch in C++
