Skip to content

Find index of element in array Python

  • by

Use the list index() method to Find the index of an element in the array Python. It returns the index in the list were the first instance of the value passed in is found.

list.index(element)

It returns the index of the first occurrence of the element that you want to find, or -1 if the element is not found.

Find the index of an element in array Python

Simple example code.

animals = ['cat', 'dog', 'rabbit', 'horse']

# get the index of 'dog'
index = animals.index('dog')
print("dog", index)

print("rabbit", animals.index('rabbit'))

Output:

Find index of element in array Python

Do comment if you have any doubts or suggestions on this Python Array.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.