Python List index() Method
Python index() method returns index of the passed element. This method takes an argument and returns index of it. If the element is not present, it raises a ValueError.
If list contains duplicate elements, it returns index of first occurred element.
This method takes two more optional parameters start and end which are used to search index within a limit.
It returns None.
Let's see some examples of index() method to understand it's functionality.
Python List index() Method Example 1
Let's first see a simple example to get index of an element using index() method.
Index of p : 1
Python List index() Method Example 2
The method returns index of first element occurred, if the element is duplicate. See the following example.
Index of l : 3 Index of p : 1
Python List index() Method Example 3
The index() method throws an error ValueError if the element is not present in the list. See the example below.
index = apple.index('z') ValueError: 'z' is not in list
Python List index() Method Example 4
Apart from element, the method takes two more optional parameters start and end. These are used to find index within a limit.
Index of p : 2 Index of a : 0
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week