OneBite.Dev - Coding blog in a bite size

search for a specific element in an array in python

Code snippet on how to search for a specific element in an array in python

def search(arr, x): 
  
    for i in range(len(arr)): 
  
        if arr[i] == x: 
            return i 
  
    return -1

This code allows you to search for a specific element in an array. The search begins by looping through the array (arr) and looking for a match between the array element and the element you are searching for (x). If the two match, the index of the element is returned. If none of the array elements match the search element, then -1 is returned.

python