# [Python] Find indices of elements in a list or array

There are many ways to find indices of a given list of elements in a big list. Some methods are slow if the list that you need to scan is huge, and some do not return indices in the correct order. You should be aware of different approaches so that you can use one of them as per the requirement. In this post, I will show you four different methods that you can use to find indices of elements in a list.

Here are my lists – ‘a’ and ‘b’. I will search the position of elements of list ‘b’ in list ‘a’.

``````>>> import numpy as np
>>> a=np.unique(np.random.randint(1, 99, 20))
>>> a
array([10, 20, 21, 24, 26, 31, 41, 44, 46, 50, 53, 55, 61, 69, 72, 81, 86,
92, 93])
>>> b=[44,24,21,61,46]``````

## Using the Numpy method in1d()

This method tests whether each element of a 1-D array is also present in another array. It returns boolean values as true (if the element is present) and false (if the element is not present). Then you can apply the nonzero() method to get the value of the indices. This method will not preserve the order of the elements you are searching for in another list.

``````>>> np.in1d(a,b)
array([False, False,  True,  True, False, False, False,  True,  True,
False, False, False,  True, False, False, False, False, False,
False])
>>> np.in1d(a,b).nonzero()[0]
array([ 2,  3,  7,  8, 12])``````

## Using the Numpy method isin()

This method tests whether each element of an array is also present in another array. It also returns boolean values as true (if the element is present) and false (if the element is not present). You can apply the nonzero() method to get the value of the indices. This method will also not preserve the order of the elements you search for in another list.

``````>>> np.isin(a,b)
array([False, False,  True,  True, False, False, False,  True,  True,
False, False, False,  True, False, False, False, False, False,
False])
>>> np.isin(a,b).nonzero()[0]
array([ 2,  3,  7,  8, 12])``````

## list index() method

The index() method returns the position of a given element in a list. If you have to find indices of many elements in a big list, it can be very slow. Also, you cannot apply this method to the Numpy array. You need to convert the Numpy array to a list before using this method. This method preserves the order of the elements you search for in another list.

``````>>> [list(a).index(v) for v in b]
[7, 3, 2, 12, 8]``````

## Converting the list to dict

Using the list that you need to scan to find the indices, you can create a dictionary with list elements as keys and their positions in the list as values. Once you have the dictionary, you can easily find the index of an element using it as a key. This method is very fast when you need to find indices of many elements in a big list. This method also preserves the order of the elements you search for in another list.

``````>>> a_dict=dict(zip(a, range(len(a))))
>>> a_dict
{10: 0, 20: 1, 21: 2, 24: 3, 26: 4, 31: 5, 41: 6, 44: 7, 46: 8, 50: 9, 53: 10, 55: 11, 61: 12, 69: 13, 72: 14, 81: 15, 86: 16, 92: 17, 93: 18}
>>> [a_dict[v] for v in b]
[7, 3, 2, 12, 8]``````

Please comment if you know other fast approaches.

## Similar Posts

This site uses Akismet to reduce spam. Learn how your comment data is processed.