This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
In this article, you will learn how to get index of list element in Python.
Let’s say you have a list of cardinal directions.
directions = ['North', 'South', 'East', 'West']
Get Index of List Element in Python
In order to get the index of a list element, you can use the index()
method.
directions = ['North', 'South', 'East', 'West']
# Find index of element 'East'
index = directions.index('East')
# Display index of element 'East'
print('Index of East:', index)
# The index of East: 2
Note: The index()
method functions by returning the index of the supplied list element.
The post How to Get Index of List Element in Python appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-02-28T17:30:22+00:00) How to Get Index of List Element in Python. Retrieved from https://www.scien.cx/2021/02/28/how-to-get-index-of-list-element-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.