Python List index() MethodPython 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. SignatureParametersNo parameter ReturnIt returns None. Let's see some examples of index() method to understand it's functionality. Python List index() Method Example 1Let's first see a simple example to get index of an element using index() method. Output: Index of p : 1 Python List index() Method Example 2The method returns index of first element occurred, if the element is duplicate. See the following example. Output: Index of l : 3 Index of p : 1 Python List index() Method Example 3The index() method throws an error ValueError if the element is not present in the list. See the example below. Output: index = apple.index('z') ValueError: 'z' is not in list Python List index() Method Example 4Apart from element, the method takes two more optional parameters start and end. These are used to find index within a limit. Output: Index of p : 2 Index of a : 0
Next TopicPython Lists
|
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