Open In App

Python | Indices list of matching element from other list

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Sometimes, while working with Python list, we have a problem in which we have to search for an element in list. But this can be extended to a list and need to find the exact places where elements occur in another 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 its index to the result list. 

Step by step approach ;

  1. Define two lists test_list1 and test_list2 containing some integers.
  2. Print the original lists for reference.
  3. Initialize an empty list res to store the indices of matching elements.
  4. Initialize a counter i to 0 to use for iterating over test_list1.
  5. While i is less than the length of test_list1, check if the current element at index i of test_list1 is in test_list2 by calling the count() function on test_list2.
  6. If the count is greater than 0 (i.e. the element is present in test_list2), append the current index i to the res list.
  7. Increment the counter i by 1 to move to the next element in test_list1.
  8. After the loop completes, res will contain the indices of matching elements.
  9. Print the resulting list of indices as the final output.

Python3




# 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 = 0
while (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))


Output : 

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]

Time complexity: O(n^2), where n is size of given test_list
Auxiliary space: O(m), where m is the size of the res list. 

Method #2: Using list comprehension + set() + enumerate() The combination of the 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




# 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))


Output : 

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]

Time complexity: O(n), where n is the length of test_list1.
Auxiliary space: O(m), where m is the length of test_list2.

Method #3: Using a dictionary

Create a dictionary with the elements in the first list as keys and their indices as values. Then, iterate through the second list and check if the elements exist in the dictionary. If they do, append their index from the dictionary to the result list.
 

Python3




# Python3 code to demonstrate working of
# Indices list of matching element from other list
# Using a dictionary
 
# 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 a dictionary
index_dict = {val: index for index, val in enumerate(test_list1)}
res = [index_dict[val] for val in test_list2 if val in index_dict]
 
# printing result
print("The matching element Indices list : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy


Output

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]

Time complexity: O(n) where n is the number of elements in the first list
Auxiliary Space: O(n)

Method #4: Using the Numpy library

Numpy library provides a function called “where” that can be used to find the indices of matching elements from the other list.

Python3




import numpy as np
 
# 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 numpy where()
arr1 = np.array(test_list1)
arr2 = np.array(test_list2)
res = np.where(np.isin(arr1, arr2))[0]
 
# printing result
print("The matching element Indices list : " + str(res))


OUTPUT:

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]

Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), The code creates two numpy arrays arr1 and arr2 of size n, where n is the length of the input lists.

Method #5: Using the built-in function map() and lambda function

This method uses the built-in function map() and a lambda function to apply the index() method of the first list to each element in the second list. The resulting iterator is converted to a list and printed. However, note that this method may not work correctly if there are duplicate elements in the first list.

Python3




test_list1 = [5, 7, 8, 9, 10, 11]
test_list2 = [8, 10, 11]
 
res = list(map(lambda x: test_list1.index(x), test_list2))
print("The matching element Indices list : " + str(res))


Output

The matching element Indices list : [2, 4, 5]

Time complexity: O(n*m), where n is the length of test_list1 and m is the length of test_list2. 
Auxiliary space: O(m), where m is the length of test_list2. 

Method 6: Using Set Intersection.

Algorithm:

  1. Create a set of unique elements from test_list1 and test_list2 using set() method.
  2. Get the intersection of both the sets using intersection() method and convert the result into set().
  3. Iterate over the set of common elements and get their indices in test_list1 using index() method and append to result list.
  4. Return the result list.

Python3




test_list1 = [5, 7, 8, 9, 10, 11]
test_list2 = [8, 10, 11]
 
res = list(set([test_list1.index(x)
                for x in set(test_list1).intersection(test_list2)]))
print("The matching element Indices list : " + str(res))


Output

The matching element Indices list : [2, 4, 5]

Time Complexity: O(n), where n is the length of test_list1.
set() method takes O(n) time to create a set of unique elements.
intersection() method takes O(min(len(test_list1), len(test_list2))) time to get the common elements.
index() method takes O(1) time to get the index of an element in test_list1.
Iterating over the set of common elements takes O(k) time, where k is the number of common elements.
Therefore, the overall time complexity is O(n + k).

Space Complexity: O(n), where n is the length of test_list1.
A set of unique elements is created which takes O(n) space.
The result list takes O(k) space, where k is the number of common elements.
Therefore, the overall space complexity is O(n + k).



Last Updated : 08 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads