Find the Last Element of a List in Python Using for Loop
(Last Updated On: 27/02/2023)
Python Program to Find the Last Element of a List Using for Loop
# Get the Last Element of the List in python Using For loop
# input list
inputList = [5, 1, 6, 8, 3]
# printing input list
print("Input list:", inputList)
# Traversing till the length of the list
for k in range(0, len(inputList)):
# Checking whether the iterator value is equal to the length of list -1(last element)
# Here == is used to check whether the iterator value is equal to list length-1
if k == (len(inputList)-1):
# Printing the corresponding element of the list,
# if the condition is true
print("Last element of the input list = ", inputList[k])
Output:
Input list: [5, 1, 6, 8, 3]
Last element of the input list = 3