A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-list-index/ below:

Python List index() - Find Index of Item

Python List index() - Find Index of Item

Last Updated : 27 Apr, 2025

index() method in Python is a helpful tool when you want to find the position of a specific item in a list. It works by searching through the list from the beginning and returning the index (position) of the first occurrence of the element you're looking for. Example:

Python
a = ["cat", "dog", "tiger"]
print(a.index("dog"))

Explanation: index("dog") method finds the first occurrence of "dog" in the list a. Since "dog" is at index 1, it returns 1.

Syntax of List index() method

list.index(element, start, end)

Parameters:

Returns:

Examples of List index()

Example 1: In this example, we are searching for the index of the number 40 within a specific range of the list from index 4 to 7 .

Python
a = [10, 20, 30, 40, 50, 40, 60, 40, 70]

res = a.index(40, 4, 8)
print(res)

Example 2: In this example, we try to find the index of 'yellow' in the list and handle the error with a try-except block if it's not found.

Python
a = ['red', 'green', 'blue']

try:
    index = a.index('yellow')
    print(a)
except ValueError:
    print("Not Present")

Example 3: In this example, we are finding the index of the tuple ("Bob", 22) in a list of tuples and index() will return the position of its first occurrence.

Python
a = [("Alice", 21), ("Bob", 22), ("Charlie", 20), ("Bob", 24)]

res = a.index(("Bob", 22))
print(res)

Also Read:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4