How to Find Value in Array in Python

To find a value in an array in Python, use the list.index() method. The list.index() is a built-in Python method that returns the index of the specified element in the list.

In Python, we use the list as the array. A list is a data structure used to store sequential data values. We can store several elements in the list. We can find a value in an array using several techniques.

arr = [1, 2, 3, 4, 5, 6, 7, 8]
searchValue = 5
index = arr.index(searchValue)
print(" The element", searchValue, " is found at: ", index)

Output

 The element 5 is found at: 4

In this program, we used the same array and value for the searchValue. However, we used the index() function to find the element’s index in this example. The index() function takes the argument as the elements needed to be found in the list.

It returns an integer, the index where the element we are searching is present. For example, the index() function finds the index of element 5 in the arr list. Therefore, the index is returned as 4.

Let’s change the code slightly.

arr = [1, 2, 3, 4, 5, 6, 7, 8]
searchValue = 20
index = arr.index(searchValue)
print("The element", searchValue, " is found at: ", index)

Output

ValueError: 20 is not in list

If we execute this program, an error is thrown. This error is called the ValueError. This is raised because the element we are searching for is not present in the list; hence this error is raised. To solve the ValueError, we can see the next by which we can solve this error.

Find array value using while loop

The while is a built-in loop that can execute a set of statements as long as a condition is true.

arr = [1, 2, 3, 4, 5, 6, 7, 8]
searchValue = 5
index = 0

while index < len(arr):
  if(arr[index] == searchValue):
     print("The element", searchValue, " is found at: ", index)
     break
  index += 1

In this program, we created an array consisting of values from 1 to 8. Next, we assigned the value that needed to be found as 5 in the searchValue variable. Then, we initialized the index variable as 0. This is used for iterating.

Inside the while, the array element is compared with the searchValue variable. If the value present in the index is equal to the searchValue element, then the index is printed, and the program comes out of the while loop.

If the element present in the index is not equal to the searchValue element then the index is incremented.

Program for finding a value in an array using index() function

arr = [1, 2, 3, 4, 5, 6, 7, 8]
searchValue = 20

if(searchValue in arr):
   index = arr.index(searchValue)
   print("The element", searchValue, " is found at: ", index)
else:
   print("The element you are searching for here is not present in the array")

Output

The element you are searching for here is not present in the array

If we execute this program, we can see the output as The element you are searching for here is not present in the array. Hence, we solved the ValueError. In function checks, whether the element is present or not; therefore, first, we find whether the element is present in the list.

We can use the index() function to print it if it is present. On the other hand, if the element is not present in the list, we can print that the element is not present.

That’s it for this tutorial.

Related posts

How to Sort 2D Array in Python

How to Save Array to File in Python

TypeError: Only size-1 arrays can be converted to Python scalars

Leave a Comment