Finding List Element Index in Python

To find the index of an element in a Python list, you can use the list.index(element, start, end) method. The list.index() method takes an element as an argument and returns the index of the first occurrence of the matching element. If the element is not found, a ValueError exception will be thrown. The optional "start" and "end" arguments restrict the search to a specific subsequence of the list. In Python, list indexes start at 0. You can also check if an element exists in a list using the "in" operator. In this Python List Index example, we get the index of a list item using the list.index() method. Below are more detailed examples of finding the index of an element in a Python list. Click Execute to run the Python List Index Example online and see the result.
Finding List Element Index in Python Execute
my_list = ["mango", "orange", "grape"]

print(my_list.index("orange"))
Updated: Viewed: 6230 times

What is Python?

Python programming language is a general-purpose language that is commonly used for server-side programming, machine learning, application testing, and as "glue code" to connect various components. Python is an interpreted and object-oriented language with dynamic binding, dynamic binding, and built-in garbage collection. Python works on most operating systems, including Windows, Linux, and macOS. Python is easy to maintain, and Python code is easy to learn. Python has built-in modules for handling JSON, strings, HTTP requests, and XML right out of the box.

What is the List in Python?

Python lists are data structures that contain an unordered list of elements. Lists can contain elements of various types, such as integers, floats, strings, classes, and even other lists. A Python list is mutable, which means that it can be changed by adding or removing elements or by sorting. The list size is not fixed and changes every time you add or remove items to/from the list.

Python List Index Method Syntax

Following is the syntax of list.index() method:

Python List index() Syntax
list.index(element, start, end)

Where:
  • element: the element to search
  • start (optional): the index to start searching
  • end (optional): the index to complete the search

The following is an example of finding the index of an element in a Python list:

Python List index() Example
my_list = ["maths", "physics", "informatics", "chemistry"]

print(my_list.index("informatics"))

# output: 2

How to find an element in a list using the "in" operator?

To check if an element exists in a list, you can use the "in" operator in Python. The in operator will return True if the element exists in the list; otherwise, it will return False.

Following is the syntax for using the in operator to check if an element is in a list.

Python in Operator Syntax
element in list

Where:
  • element: the element to be found in the list
  • list: the list in which the element will be searched

Unlike the list.index() method, the "in" operator does not return the element's index and does not throw an exception.

Find the Element in the Python List using in Operator
my_list = ['Opera', 'Chrome', 'Firefox', 'Safari']

if 'Chrome' in my_list:
 print("Found")
else:
 print("Not found")

# output: Found

See also