Open In App

Python | Get match indices

Last Updated : 03 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with lists we need to handle two lists and search for the matches, and return just the indices of the match. Querying the whole list for this process is not feasible when the size of master list is very large, hence having just the match indices helps in this cause. Let’s discuss certain ways in which this can be achieved. 

Method #1 : Using list comprehension + index() This problem can potentially be solved using the index function of python to get the wanted indices and list comprehension can be used to extend this to the whole string. 

Python3




# Python3 code to demonstrate
# Get match indices
# using list comprehension and index()
 
# initializing lists
test_list1 = [5, 4, 1, 3, 2]
test_list2 = [1, 2]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# using list comprehension and index()
# Get match indices
res = [test_list1.index(i) for i in test_list2]
 
# print result
print("The Match indices list is : " + str(res))


Output : 

The original list 1 : [5, 4, 1, 3, 2]
The original list 2 : [1, 2]
The Match indices list is : [2, 4]

Time Complexity: O(n^2), where n is the length of test_list2.

Auxiliary Space: O(n)

Method #2 : Using enumerate() + list comprehension The enumerate function can be used to produce the key value pair for the list being it’s index and value and we can store them using list comprehension. 

Python3




# Python3 code to demonstrate
# Get match indices
# using list comprehension and enumerate()
 
# initializing lists
test_list1 = [5, 4, 1, 3, 2]
test_list2 = [1, 2]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# using list comprehension and enumerate()
# Get match indices
res = [key for key, val in enumerate(test_list1)
                      if val in set(test_list2)]
 
# print result
print("The Match indices list is : " + str(res))


Output : 

The original list 1 : [5, 4, 1, 3, 2]
The original list 2 : [1, 2]
The Match indices list is : [2, 4]

Method #3 : Using filter()

Here is another approach using the filter function to get the indices of the matching elements:

Python3




# initializing lists
test_list1 = [5, 4, 1, 3, 2]
test_list2 = [1, 2]
 
# printing original lists
print("The original list 1 :", test_list1)
print("The original list 2 :", test_list2)
 
# use filter to get the indices of the matching elements
res = list(filter(lambda x: test_list1[x] in test_list2, range(len(test_list1))))
 
# print result
print("The Match indices list is :", res)
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list 1 : [5, 4, 1, 3, 2]
The original list 2 : [1, 2]
The Match indices list is : [2, 4]

In the given code, we are initializing two lists test_list1 and test_list2. Then, we are printing the original lists.

Next, we are using the filter function to get the indices of the elements in test_list1 that are present in test_list2. The filter function takes a function and an iterable as input, and returns an iterator that only contains the elements for which the function returns True.

In this case, the function passed to filter is a lambda function that takes an index x and returns True if the element at index x in test_list1 is present in test_list2. The iterable passed to filter is the range object range(len(test_list1)), which generates a sequence of integers from 0 to len(test_list1) – 1.

Finally, we are converting the iterator returned by filter to a list using the list function, and printing the resulting list of indices.

Method #4: Using a for loop

  • Initialize an empty list, say res_list, to store the indices of matching elements.
  • Iterate over each element in test_list1 and check if it is present in test_list2.
  • If it is present, append the index of that element in test_list1 to res_list.
  • Return res_list

Python3




# initializing lists
test_list1 = [5, 4, 1, 3, 2]
test_list2 = [1, 2]
 
# printing original lists
print("The original list 1 :", test_list1)
print("The original list 2 :", test_list2)
 
# using for loop to get the indices of the matching elements
res_list = []
for i in range(len(test_list1)):
    if test_list1[i] in test_list2:
        res_list.append(i)
 
# print result
print("The Match indices list is :", res_list)


Output

The original list 1 : [5, 4, 1, 3, 2]
The original list 2 : [1, 2]
The Match indices list is : [2, 4]

Time complexity: O(n), where n is the length of test_list1. 
Auxiliary space: O(k), where k is the number of matching elements in test_list1.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads