Posted on 16th September 2024|15 views
How can I get the last element of the list in python?
Posted on 16th September 2024| views
You can simply find the last digit of a list by using [] operator just like this.
test_list = [1, 3, 8, 6, 4, 5]
# printing original list
print ("This is the original list : " + str(test_list))
# using len - 1 index to print last list element
print ("Finding the last element by using [ len -1 ] is : "
+ str(test_list[len(test_list) -1]))
# using -1 index to print last list element
print ("Finding the last element by using [ -1 ] is : "
+ str(test_list[-1]))
Output:
This is the original list : [1, 3, 8, 6, 4, 5]
Finding the last element by using [ len -1 ] is : 5
Finding the last element by using [ -1 ] is : 5