Find the Last Element of a List in Python Using Negative Indexing

(Last Updated On: 23/02/2023)

Python Program to Find the Last Element of a List Using Negative Indexing

# Get the Last Element of the List in python Using negative indexing

# input list
inputList = [5, 1, 6, 8, 3]

# printing input list
print("Input list:", inputList)

# getting the last element of the list using the length of the list - 1 as an index(Here list index starts from 0 so list length -1 gives the index of the last element of the list)
print("Last element of the input list using len(list)-1 as index = ", inputList[len(inputList) - 1])

# getting the last element of the list using - 1 as the index
print("Last element of the input list using -1 as index = ", inputList[-1])

Output:

Input list: [5, 1, 6, 8, 3]
Last element of the input list using len(list)-1 as index =  3
Last element of the input list using -1 as index =  3

Leave a Reply

Your email address will not be published. Required fields are marked *