Find index of same element in list Python

This post will discuss how to find the index of all occurrences of an item in a Python List.

1. Using enumerate[] function

To get the index of all occurrences of an element in a list, you can use the built-in function enumerate[]. It was introduced to solve the loop counter problem and can be used as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if __name__ == '__main__':
ints = [1, 3, 7, 5, 4, 3]
item = 3
for index, elem in enumerate[ints]:
if elem == item:
print[f"{item} is found at index {index}"]
'''
Output:
3 is found at index 1
3 is found at index 5
'''

DownloadRun Code


To get the list of all indices at once, use list comprehension with enumerate function:

1
2
3
4
5
6
7
8
9
10
if __name__ == '__main__':
ints = [1, 3, 7, 5, 4, 3]
item = 3
indexes = [i for i, j in enumerate[ints] if j == item]
print[f"Item {item} is found at index {indexes}"]
# Output: Item 3 is found at index [1, 5]

DownloadRun Code

2. Using range[] function

Alternatively, you can use the range[] function to get the list of all valid indices and then match the corresponding value present at each index with the given item.

1
2
3
4
5
6
7
8
9
10
if __name__ == '__main__':
ints = [1, 3, 7, 5, 4, 3]
item = 3
indexes = [i for i in range[len[ints]] if ints[i] == item]
print[f"Item {item} is found at index {indexes}"]
# Output: Item 3 is found at index [1, 5]

DownloadRun Code

3. Using itertools module

Another approach is to use the count[] function in itertools, which creates an iterator for efficient looping to return evenly spaced values starting with the given number. You can use it with the zip[] function to add sequence numbers in the following manner.

1
2
3
4
5
6
7
8
9
10
11
12
from itertools import count
if __name__ == '__main__':
ints = [1, 3, 7, 5, 4, 3]
item = 3
zipped = [[i, j] for i, j in zip[count[], ints] if j == item]
print[zipped]
# [[1, 3], [5, 3]]

DownloadRun Code

4. Using more_itertools module

The more-itertools library provides elegant routines to work with Python iterables. You can use the more_itertools.locate function that yields the index of each item in iterable for which satisfies the given predicate. Following is a simple example demonstrating usage of this function:

1
2
3
4
5
6
7
8
9
10
11
12
from more_itertools import locate
if __name__ == '__main__':
ints = [1, 3, 7, 5, 4, 3]
item = 3
indexes = list[locate[ints, lambda x: x == item]]
print[f"Item {item} is found at index {indexes}"]
# Output: Item 3 is found at index [1, 5]

Download Code

5. Using NumPy Library

Finally, if you happen to be using NumPy already and need all indexes, you may use the numpy.where[] function.

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
if __name__ == '__main__':
ints = [1, 3, 7, 5, 4, 3]
item = 3
indexes = np.where[np.array[ints] == item][0]
print[f"Item {item} is found at index {indexes}"]
# Output: Item 3 is found at index [1, 5]

Thats all about finding the index of all occurrences of an item in a list in Python.

Video liên quan

Chủ Đề