xxxxxxxxxx
# Python code to access the last element
# in a list using reverse() method
# Declaring and initializing list
number_list = [2, 1, 7, 4, 5, 3,6]
print ("List of elements are : " + str(number_list))
# using reverse method to print last element
number_list.reverse()
print("The last element of list using reverse method are : "
+ str(number_list[0]))
xxxxxxxxxx
my_list = ['red', 'blue', 'green']
# Get the last item with brute force using len
last_item = my_list[len(my_list) - 1]
# Remove the last item from the list using pop
last_item = my_list.pop()
# Get the last item using negative indices *preferred & quickest method*
last_item = my_list[-1]
# Get the last item using iterable unpacking
*_, last_item = my_list