Python | Indices list of matching element from other list
Sometimes, while working with Python list, we have a problem in which we have to search for a element in list. But this can be extended to a list and need to find the exact places where elements occur in other list. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop + count()
This is the brute method to perform this task. In this we can just count the occurrence of an element in other list and if we find it, then we add it’s index to the result list.
# Python3 code to demonstrate working of # Indices list of matching element from other list # Using loop + count() # initializing lists test_list1 = [5, 7, 8, 9, 10, 11] test_list2 = [8, 10, 11] # printing original lists print("The original list 1 is : " + str(test_list1)) print("The original list 2 is : " + str(test_list2)) # Indices list of matching element from other list # Using loop + count() res = [] i = 0while (i < len(test_list1)): if (test_list2.count(test_list1[i]) > 0): res.append(i) i += 1 # printing result print("The matching element Indices list : " + str(res)) |
The original list 1 is : [5, 7, 8, 9, 10, 11] The original list 2 is : [8, 10, 11] The matching element Indices list : [2, 4, 5]
Method #2 : Using list comprehension + set() + enumerate()
The combination of above functions can be used to perform the task. In these we just convert the list to set and then check for index and value together for existence using enumerate() and append the index if found.
# Python3 code to demonstrate working of # Indices list of matching element from other list # Using list comprehension + set() + enumerate() # initializing lists test_list1 = [5, 7, 8, 9, 10, 11] test_list2 = [8, 10, 11] # printing original lists print("The original list 1 is : " + str(test_list1)) print("The original list 2 is : " + str(test_list2)) # Indices list of matching element from other list # Using list comprehension + set() + enumerate() temp = set(test_list2) res = [i for i, val in enumerate(test_list1) if val in temp] # printing result print("The matching element Indices list : " + str(res)) |
The original list 1 is : [5, 7, 8, 9, 10, 11] The original list 2 is : [8, 10, 11] The matching element Indices list : [2, 4, 5]
Recommended Posts:
- Python | Duplicate element indices in list
- Python | Indices of sorted list of list elements
- Python | Indices of N largest elements in list
- Python | Find indices with None values in given list
- Python | Ways to find indices of value in list
- Python | Find elements of a list by indices
- Python | Find dictionary matching value in list
- Python | Replace elements in second list with index of same element in first list
- Python | Group elements at same indices in a multi-list
- Python | Get indices of True values in a binary list
- Python | Group by matching second tuple value in list of tuples
- Python program to fetch the indices of true values in a Boolean list
- Python | Move one list element to another list
- Python | Slicing list from Kth element to last element
- Python | Convert list of string to list of list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



