Python Program to find the index of the first occurrence of the specified item in the array


An array is a data structure that is used to store elements of the same data type in order. And the stored elements are identified by an index value. Python doesn’t have a specific data structure to represent arrays. However, we can use the List data structure or Numpy module to work with arrays.

In this article, we see multiple ways to get the index of the first occurrence of a specified item in the array.

Input Output Scenarios

Let us now look at some input output scenarios.

Assume we have an input array with few elements. And in the output, we will get the index of a specified value of its first occurrence.

Input array:
[1, 3, 9, 4, 1, 7]
specified value = 9
Output:
2

The specified element 9 is present in the array in a single occurrence only, and the resultant index of that value is 2.

Input array:
[1, 3, 6, 2, 4, 6]
specified value = 6
Output:
2

The given element 6 has occurred twice in the array, and the first occurrence index value is 2.

Using the list.index() method

The list.index() method helps you to find the index of the first occurrence of a given element in an array. If there are duplicate elements inside the list, the first index of the element is returned. Following is the syntax –

list.index(element, start, end)

The first parameter is the element that we want to get the index, and the second and third parameters are the optional parameters where to start and end our search for the given element.

The list.index() method returns an integer value, it is the index of the given element that we passed to the method.

Example

In the example above, we will use the index() method.

# creating array
arr = [1, 3, 6, 2, 4, 6]
print ("The original array is: ", arr) 
print() 

specified_item = 6

# Get index of the first occurrence of the specified item
item_index = arr.index(specified_item)

print('The index of the first occurrence of the specified item is:',item_index)

Output

The original array is:  [1, 3, 6, 2, 4, 6]
The index of the first occurrence of the specified item is: 2

The given value 6 is present twice in the array but the index() method returns the index of the first occurrence value only.

Using for loop

Similarly, we can get the index of a specified item that occurred in the first position of the array using a for loop and a if condition.

Example

Here, we will iterate the array elements using a for loop.

# creating array
arr = [7, 3, 1, 2, 4, 3, 8, 5, 4]
print ("The original array is: ", arr) 
print() 

specified_item = 4
# Get the index of the first occurrence of the specified item
for index in range(len(arr)):
   if arr[index] == specified_item:
      print('The index of the first occurrence of the specified item is:',index)
      break

Output

The original array is:  [7, 3, 1, 2, 4, 3, 8, 5, 4]
The index of the first occurrence of the specified item is: 4

The given value 4 is present repeatedly in the array but the above example returns the index of the first occurred value only.

Using numpy.where()

The numpy.where() method is used to filter the array elements based on the given condition. By using this method we can get the index of a given element. Following is the syntax –

numpy.where(condition, [x, y, ]/)

Example

In this example, we will use numpy.where() method with a condition.

import numpy as np

# creating array
arr = np.array([2, 4, 6, 8, 1, 3, 9, 6])
print("Original array: ", arr)

specified_index = 6

index = np.where(arr == specified_index)
# Get index of the first occurrence of the specified item
print('The index of the first occurrence of the specified item is:',index[0][0])

Output

Original array:  [2 4 6 8 1 3 9 6]
The index of the first occurrence of the specified item is: 2

The condition arr == specified_index checks for the given element in the numpy array and returns an array with elements where the given condition is satisfied or True. From that resultant array, we can get the index of a first occurrence by using index[0][0].

Updated on: 29-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements