Python list get index of item banner

Python List Index – With Examples

In python, you can get the index of an item in a list using the index() function. Python lists are ordered collection of objects. Meaning, there’s a sequence to the elements inside the list and each element can be referred to by an index. Items in a list have zero-based indexing, that is, they are indexed starting from zero.

Before we proceed, here’s a quick refresher on python lists – Lists are used to store an ordered collection of items. These items can be any type of object from numbers to strings or even another list. This makes lists are one of the most versatile data structures in python to store a collection of objects. For more, check out our guide on lists and other data structures in python.

index() is a list function that returns the index of the first matching item.

Syntax:

sample_list.index(x, start, end)

Here, sample_list is the list from which you want to get the index of the element x.

Parameters:

  • x: The element whose index is to be found.
  • start (optional): The position inside the list to start the search from.
  • end (optional): Search the list up to (but not including) this index.

You can understand that start and end optional parameters as a means to slice the list before searching for the element.

Returns:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Returns the index of the first match. If the element is not found, a ValueError is raised.

Example 1: Index of an element in a list

# get index of list element
ls = ['apple', 'orange', 'grapes']
i = ls.index('orange')
print("The list:", ls)
print("Index of 'orange':", i)

Output:

The list: ['apple', 'orange', 'grapes']
Index of 'orange': 1

In the above example, we find the index of the element 'orange' in the list ls using the index() function which returns the index as 1.

Example 2: When the element is present more than once

# get index of list element
ls = ['apple', 'orange', 'grapes', 'bananas', 'grapes']
i = ls.index('grapes')
print("The list:", ls)
print("Index of 'grapes':", i)

Output:

The list: ['apple', 'orange', 'grapes', 'bananas', 'grapes']
Index of 'grapes': 2

In the above example, the element 'grapes' is present more than once in the list. When we use the index() function to get its index, it returns 2, which is the index of when it appears for the first time in the list starting from left.

Example 3: When the element is not present

# get index of list element
ls = ['apple', 'orange', 'grapes', 'bananas', 'grapes']
i = ls.index('mango')
print("The list:", ls)
print("Index of 'mango':", i)

Output:

ValueError                                Traceback (most recent call last)
<ipython-input-6-f1a812c4f0c3> in <module>
      1 # get index of list element
      2 ls = ['apple', 'orange', 'grapes', 'bananas', 'grapes']
----> 3 i = ls.index('mango')
      4 print("The list:", ls)
      5 print("Index of 'mango':", i)

ValueError: 'mango' is not in list

In the above example, we get a ValueError when using the index() function to get the index of 'mango', an element that is not present in the list.

Example 4: Providing the start and end indices

# get index of list element
ls = ['apple', 'orange', 'grapes', 'bananas', 'grapes']
i = ls.index('grapes', 3, 5)
print("The list:", ls)
print("Index of 'grapes':", i)

Output:

The list: ['apple', 'orange', 'grapes', 'bananas', 'grapes']
Index of 'grapes': 4

In the above example, we provide for the start and end indices which limit the search region of the list from the 3rd index up to (but not including) the 5th index. And then, we get the index of the element 'grapes' as 4.

For more on python list functions, refer to the python docs.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top