Find index of element in array in python Last Updated : 03 Dec, 2024 Comments Improve Suggest changes Like Article Like Report We often need to find the position or index of an element in an array (or list). We can use an index() method or a simple for loop to accomplish this task. index() method is the simplest way to find the index of an element in an array. It returns the index of the first occurrence of the element we are looking for. Python a = [10, 20, 30, 40, 50] # Find the index of the element 30 in the list 'a' idx = a.index(30) print(idx) Output2 Note: If the element is not found, Python raises a ValueError. We can handle this using a try and except block.Let's look into various other methods which we can use to find index of element in array in python is using a simple for loop.Table of ContentUsing for loopUsing List Comprehension and enumerate()Using numpy for Arrays Using numpy.where() for Arrays For numerical arrays, numpy provides an efficient method using numpy.where(). Python import numpy as np arr = np.array([10, 20, 30, 40, 30, 50]) element = 30 # Find indices indices = np.where(arr == element)[0] print("Indices:", indices) OutputIndices: [2 4] Using for loopWe can use a similar for with lists to search for an element by looping through the list. Here’s how we can do that: Python arr = [10, 20, 30, 40, 50] element = 40 idx = -1 for i in range(len(arr)): if arr[i] == element: idx = i break print(idx) Output3 Using List Comprehension with enumerate()Find all indices of an element using list comprehension With enumerate(). Python a = [10, 20, 30, 40, 30, 50] element = 30 # Find all indices indices = [i for i, val in enumerate(a) if val == element] print("Indices:", indices) OutputIndices: [2, 4] Comment More infoAdvertise with us Next Article Find index of element in array in python P pragya22r4 Follow Improve Article Tags : Python Python Programs python-list Python list-programs python +1 More Practice Tags : pythonpythonpython-list Similar Reads Find Index of Element in Array - Python In Python, arrays are used to store multiple values in a single variable, similar to lists but they offer a more compact and efficient way to store data when we need to handle homogeneous data types . While lists are flexible, arrays are ideal when we want better memory efficiency or need to perform 2 min read Find element in Array - Python Finding an item in an array in Python can be done using several different methods depending on the situation. Here are a few of the most common ways to find an item in a Python array.Using the in Operatorin operator is one of the most straightforward ways to check if an item exists in an array. It r 3 min read Handling " No Element Found in Index() " - Python The task of handling the case where no element is found in the index() in Python involves safely managing situations where the desired element does not exist in a list. For example, with the list a = [6, 4, 8, 9, 10] and the element 11, we want to ensure the program doesn't raise an error if the ele 3 min read Find an Element In a List of Tuples In Python programming, it is often necessary to locate an element inside a list of tuples. Tuples are arranged collections, and locating a particular piece inside them requires knowledge of a variety of strategies. When data is kept as tuples inside a list, this procedure is essential for data retri 3 min read Difference Between find() and index() in Python Both methods are used to locate the position of a substring within a string but the major difference between find() and index() methods in Python is how they handle cases when the substring is not found. The find() method returns -1 in such cases, while index() method raises a ValueError.Example of 2 min read Like