Introduction to Python List Index
In this article, we will discuss on Python list index. In Python, the list is a data structure that contains the ordered elements or sequence of elements. So each element in this list is known as the item. In this article, the list index is the position number given to each element in the given list. So to access these elements using index numbers, Python has an index method. So we will see what the index() method is and how it works. This method is used to search the given elements inside the list, and this method returns only the index value but not the element value.
Functions of Python List index() Method
In this article, we will see the list index concept in Python along with examples. In this section, we will see the index() method and its working with examples.
In Python, the list index() method is defined to search the given element or item in the list. This method returns the index position of the element or items in the list, which is in numerical value. If the same element occurs twice in the list, it will return the index of the first occurrence of the list element. In Python, the indexing of the elements in the list starts from 0 and not 1. Now let us see the syntax and its example below:
The above statement shows the syntax of the index() method, which takes only one parameter, and that is items of the given list that needs to be searched. The return value of this method returns the index value or position of the element in the list. If the element is not found in the list given, then it raises the ValueError.
This syntax can also have other parameters like as below:
list.index(element, start, end)
In the above statement, we have three parameters where in this element parameter is for the element to be searched, start parameter is optional but specifies the beginning position from where the search begins, end parameter is also optional but specifies the end position from the where the search ends.
Now let us see a simple example below that uses the index() method.
Examples to Implement Python List index() Method
Below are the examples of the Python List Index:
list1 = [1, 2, 3, 4, 1, 1, 1, 4, 5] print("The given list is as follows:") print(list1) print("The index obtained for the given index number in the method:") print(list1.index(3)) list2 = ['bat', 'cat', 'mat', 'cat', 'pet'] print("The given string list is as follows:") print(list2) print("The index obtained for the given element value in the index method") print(list2.index('cat'))
Explanation: In the above program, we can see that we have declared two lists, “list1” and “list2”. Where list1 has a list of numbers and list2 has strings. So we have specified the index() method on list1as the “3” element and its index value is “2” and the index() method specified on the list2 is “cat”, and the index value it returns is “1”. We have to note that in list2, there are two occurrences of the “cat” string, but the index it returns is the first occurrence of “cat” that is “1.”
and the second occurrence of “cat” is “3”. So the method index() returns the value “1” when “cat” is asked to find. Similarly, we can see the below example, which results in only vowels’ index values in the given list.
letters = ['a', 'e', 'i', 'o', 'i', 'u', 'k', 'h'] print("Prints the index value of vowels in the above given list:") indexa = letters.index('a') print('The index of a:', indexa) indexe = letters.index('e') print('The index of e:', indexe) indexi = letters.index('i') print('The index of i:', indexi) indexo = letters.index('o') print('The index of o:', indexo) indexu = letters.index('u') print('The index of u:', indexu)
Now let us see what happens when the element in the list is not found. Let us see the below example in which we will see what it results if the element is not present in the list.
list1 = [1, 2, 3, 4, 1, 1, 1, 4, 5] print("The given list is as follows:") print(list1) print("\n") print("The error occured is as below:") print(list1.index(10))
Explanation: In the above program, we can see that the given list has a certain set of numbers, and we are trying to print the element “10”, which is not present in the list. Hence the result will display “ValueError”, which gives the message saying the “10” is not present in the list.
Now let us see how the second syntax works with the below example, which has start and end parameters.
list1 = [1, 9, 3, 4, 1, 1, 4, 4, 5] print("The given list is as follows:") print(list1) print("\n") print("The index of the given element with start and end parameter for index method:") print(list1.index(4, 3, 8))
Explanation: In the above program, we can see the given list has the element “4” with different indexes, and we have specified in the method with starting index “3” and end index “8” in between this index it will give the index of which has the first occurrence of “4” element. Hence the first occurrence element “4” between indexes 3 to 8 is “3”.
In this article, we saw what an index in the Python list is. We also saw how we used the index() method to search the element given or passed to the index method. This method returns the index value of the element given in the method. We also saw another syntax and example in which we specify the start index and end index so that we can print the index value of the element with its first occurrence. We also saw the example of what the result will be if the element is not present in the list, then it will give the ValueError: element, not in the list.
This is a guide to Python List Index. Here we discuss a brief overview of the Python List index() method and its Examples, along with its Code Implementation. You can also go through our other suggested articles to learn more –