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.
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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
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.