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.
The index() function
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.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
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:
Returns the index of the first match. If the element is not found, a ValueError
is raised.
Examples
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.
Tutorials on python lists:
- Python – Check if an element is in a list
- Python – Iterate over multiple lists in parallel using zip()
- Python – Flatten a list of lists to a single list
- Pandas DataFrame to a List in Python
- Python – Convert List to a String
- Convert Numpy array to a List – With Examples
- Python List Comprehension – With Examples
- Python List Index – With Examples
- Python List Count Item Frequency
- Python List Length
- Python Sort a list – With Examples
- Python Reverse a List – With Examples
- Python Remove Duplicates from a List
- Python list append, extend and insert functions.
- Python list remove, pop and clear functions.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.