Python list index -1 Tutorial: Complete Guide

0
50
Python list index -1 Tutorial with Example

Python list index starts with 0. The list index() is a built-in Python method that returns the index of the element in the list.

Python list index -1

Python list index -1 returns the last element of the list. The index -2 indicates the second last element of the list. Index 0 represents the first element of the list, and index -1 means the last element of the list.

list = [11, 21, 19, 46]

print(list[-1])

Output

46

You can see that the index -1 returns the last element of the Python list. If any item not present in the list is searched, it returns the ValueError. If you want to get the first element of the list, use the 0th index of the list.

list = [11, 21, 19, 46]

print(list[0])

Output

11

The 0th index returns the first element of the list.

That’s it for this tutorial.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.