Find the index of an item in a list in Python

Finding the index of an item in a list: In this tutorial, we will learn how to find the index of a given item in a Python list. Learn with the help of examples. By Sapna Deraje Radhakrishna Last updated : June 26, 2023

Given a Python list and an item, we have to find the index of the given item in the list. Consider the below example with sample input/output to understand the task better -

Input: ['bangalore','chennai','mangalore','vizag','delhi']
Item: 'bangalore'
Output: Index is 0

Input: [1,2,3,4,5]
Item: 4
Output: Index is 3

How to find the index of an item in a Python list?

To find the index of an item in a Python list, you can use the list.index() method by passing the item whose index is to be found. If the item exists in the list, it returns the index; ValueError, otherwise.

Syntax

list.index(x[, start[, end]])

The index() returns zero-based index in the list of the first item whose value is equal to x and raises a ValueError if there is no such item.

Argument(s)

  • x = item whose lowest index will be returned
  • start and end (optional) = used to limit the search to a particular subsequence of the list

Find the index of an item in a list without providing optional arguments

In this example, we have a list of cities. We have to find the index of the specific city in the given list. In this approach, we will not use the optional arguments.

# String list
cities = ["bangalore", "chennai", "mangalore", "vizag", "delhi"]

# Item to be found
item = "vizag"

# Finding the index of "vizag"
index = cities.index(item)

# Print the result
print(item, "is at index", index)

Output

vizag is at index 3

Find the index of an item in a list using start and end limit

In this example, we have a list of cities. We have to find the index of the specific city in the given list between the given indices i.e., between the given start and end indices. In this approach, we will use both of the optional arguments (start, end).

# String list
cities = ["bangalore", "chennai", "mangalore", "vizag", "delhi"]

# Item to be found
item = "chennai"

# Finding the index of "chennai" between
# the indices 0 to 3
index = cities.index(item, 0, 3)

# Print the result
print(item, "is at index", index)
print()

item = "vizag"
# Finding the index of "vizag" between
# the indices 0 to 3
index = cities.index(item, 0, 3)

# Print the result
print(item, "is at index", index)

Output

chennai is at index 1

Traceback (most recent call last):
  File "/home/main.py", line 18, in <module>
    index = cities.index(item, 0, 3)
ValueError: 'vizag' is not in list

Find the index of an item in a list having multiple frequencies

When we have multiple frequencies of an item to be found, the index() method returns the index of the first occurrence.

# String list
cities = ["bangalore", "chennai", "mangalore", "chennai", "delhi"]

# Item to be found
item = "chennai"

# Finding the index of "chennai"
index = cities.index(item)

# Print the result
print(item, "is at index", index)

Output

chennai is at index 1

Find the indices of all occurrences of an item in a list

To find the indices of all occurrences of a given item in a list, you can use enumerate() method which works with iterable and returns an enumerate object.

# String list
cities = ["bangalore", "chennai", "mangalore", "chennai", "delhi"]

# Item to be found
item = "chennai"
index = -1

for i, j in enumerate(cities):
    if j == item:
        print(item, "is at index", i)

Output

chennai is at index 1
chennai is at index 3

Python List Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.