Python List index()
Python List index()
Python List index()
Python List index() method returns the index of the element, that is passed as argument, in the list. If the element is not at all present in the list, then it throws ValueError. In this tutorial, we will learn how to use index() method, when there is one occurrence, multiple occurrences or no occurrence of the element in the list.
Syntax ? list.index()
The syntax of list.index() method is myList.index(element)
where myList is the list of elements. index is the name of the method. element is the one whose index in the list, the method returns.
Example 1 ? Python list.index()
In this example, we will take a list of strings, say fruit names, and find the index of mango in the list. The following screenshot depicts the list and how index() method finds a match for the element and returns its index.
myList
apple banana cherry orange mango kiwi
0
1
2
3
4
5
myList.index( mango )
return value
Python Program
myList = ['apple', 'banana', 'cherry', 'orange', 'mango', 'kiwi'] ind = myList.index('mango') print(ind)
Output
4
list.index() ? Multiple Occurrences of Element
When the element, whose index we are trying to find, occurs multiple times in the list, list.index() method returns the index of first match and ignores the other occurrences.
myList
apple banana mango cherry orange mango kiwi
0
1
2
3
4
5
6
myList.index( mango )
return value
Python Program
myList = ['apple', 'banana', 'mango', 'cherry', 'orange', 'mango', 'kiwi'] ind = myList.index('mango') print(ind)
Output 2
list.index() ? Get All Occurrences of Element
You can use list.index() along with slicing technique to get all the occurrences of the element in the list.
In the following program, we have list of strings, and we shall find all the occurrences of the string mango in the list.
The logic is that, each time we get the index of element in the list, we slice the list from that index and find the next occurrence of the element. We collect these indexes in a list.
Python Program
myList = ['apple', 'banana', 'mango', 'cherry', 'orange', 'mango', 'kiwi'] element = 'mango' indexes = []
while True: try: slicingPos = 0 if len(indexes) == 0 else indexes[len(indexes)-1]+1 index = myList[slicingPos:].index(element) indexes.append(index+slicingPos) except ValueError as e: break
print(indexes)
Output
[2, 5]
list.index() ? No Occurrences of Element
When there is no occurrence of element, whose index we are trying to find in the list, list.index() throws ValueError.
Python Program
myList = ['apple', 'banana', 'cherry', 'orange', 'mango', 'kiwi'] ind = myList.index('watermelon') print(ind)
Output
Traceback (most recent call last): File "d:/workspace/python/example.py", line 2, in ind = myList.index('watermelon')
ValueError: 'watermelon' is not in list
This could terminate our program execution. So, how do we handle this?
Prior to making a call to index() method, check if element is present in the list.
Python Program
myList = ['apple', 'banana', 'cherry', 'orange', 'mango', 'kiwi'] element = 'watermelon'
if element in myList: ind = myList.index('watermelon') print(ind)
else: print(element, 'not in the list.')
Output
watermelon not in the list.
Or you can also use try except to handle ValueError thrown by list.index() method.
Python Program
myList = ['apple', 'banana', 'cherry', 'orange', 'mango', 'kiwi'] element = 'watermelon' try:
ind = myList.index('watermelon') print(ind) except ValueError as e: print(element, 'not in the list.')
Output
watermelon not in the list.
Conclusion
In this Python Tutorial, we learned the syntax of list.index() method and how to use it in different scenarios in your Python programs.
Python Programming
Python Tutorial Install Python Install Anaconda Python Python HelloWorld Program Python Variables Python Variable Data Type Conversion Python Comments
Control Statements
Python If Python If Else Python While Loop Python For Loop
Python String
Python String Methods Python String Length Python String Replace Python Split String Python Count Occurrences of Sub-String Python Sort List of Strings
Functions
Python Functions
Python Collections
Python List Python Dictionary
Advanced
Python Multithreading
Useful Resources
Python Interview Questions
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- python list index
- index of python programs
- pandas dataframe notes university of idaho
- chapter 2 lists arrays and dictionaries uc davis
- python tutorial list and negative indexing iit guwahati
- chapter 15 javascript 4 objects and arrays university of cape town
- interfacing python and c using ctypes international centre for
- vlookup lookup value table array col index num range lookup
- runtime compilation of array oriented python programs new york university
- python for bioinformatics