21
loading...
This website collects cookies to deliver better user experience
# Python code to access the last element
# in a list using for loop
# Declaring and initializing list
number_list = [2, 1, 7, 4, 5, 3,6]
print ("List of elements are : " + str(number_list))
# using loop method to print last element
for i in range(0, len(number_list)):
if i == (len(number_list)-1):
print ("The last element of list is : "+ str(number_list[i]))
# Output
List of elements are : [2, 1, 7, 4, 5, 3, 6]
The last element of list is : 6
reverse()
method in Python is used to reverse the elements of a list. The reverse()
method does not take any arguments and does not return any values instead, it reverses the existing list.# 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]))
# Output
List of elements are : [2, 1, 7, 4, 5, 3, 6]
The last element of list using reverse method are : 6
pop()
method is used to access the last element of the list and returns the removed item. The*pop()
* method accepts an integer as the argument, which is basically the index of the item which needs to be removed from the list.list.pop(0)
will remove and return the first element in the list. pop()
method will remove the last item from the list itself. Hence if you are re-using the list, this approach is not recommended as the element is removed.# Python code to access the last element
# in a list using pop() method
# Declaring and initializing list
number_list = [2, 1, 7, 4, 5, 3,6]
print ("List of elements are : " + str(number_list))
# using pop() method to print last element
print("The last element of list using reverse method are : "
+ str(number_list.pop()))
# Output
List of elements are : [2, 1, 7, 4, 5, 3, 6]
The last element of list using reverse method are : 6
# Python code to access the last element
# in a list using negative indexing
# Declaring and initializing list
number_list = [2, 1, 7, 4, 5, 3,6]
print ("List of elements are : " + str(number_list))
# using length-1 to print last element
print("The last element of list using length-1 method are : "
+ str(number_list[len(number_list) -1]))
# using [] operator to print last element
print("The last element of list using reverse method are : "
+ str(number_list[-1]))
# Output
List of elements are : [2, 1, 7, 4, 5, 3, 6]
The last element of list using length-1 method are : 6
The last element of list using reverse method are : 6
itemgetter()
*method is one of the methods in the operator module, and it accepts one or more integer arguments and returns the callable object.import operator
# Python code to access the last element
# in a list using itemgetter() method
# Declaring and initializing list
number_list = [2, 1, 7, 4, 5, 3,6]
print ("List of elements are : " + str(number_list))
getLastElement = operator.itemgetter(-1)
# using [] operator to print last element
print("The last element of list using reverse method are : "
+ str(getLastElement(number_list)))
# Output
List of elements are : [2, 1, 7, 4, 5, 3, 6]
The last element of list using reverse method are : 6