Python List Index is another example of Python List Methods. With this method, we get the index of the find member in the list. This python method gives the first find member’s index.
By the way, index values start with 0. It means that the index of the first item in a list is 0. The second item in a list has index 1, the third has 2 and so on. So, to reach an item in a list, we give one lower number.
To understand this better, let’s do an example and for Python Index Method. Below, we will get the index of member 45.
numbers = [5,9,45,7,126,4]
x = numbers.index(45)
print(x)
The output will be 2, because “45” is the third member of this list, it is index 2. Remember, the indexes start from 0. This means that the first member of a list has index 0.
2
You can also learn Python List Reverse Sort
If we look fort he index of “5”, we will receive the below return.
numbers = [5,9,45,7,126,4]
x = numbers.index(5)
print(x)
This is also similar for string lists. For the below string list Python List Index example, we will receive 4 as a result. Because, “bb” is the fifth member of this list.
strings = ["aa","abc","aaa","a","bb"]
x = strings.index("bb")
print(x)
4
Python Index method is an important method for python lists. With this method, you can access any members’s index and use it for different aims. You can learn other methods of lists in this Python Programming Training.
Leave a Reply