The list is one of the most widely used python containers in programming. Therefore, it is necessary to know different operations that can be performed on the list. This tutorial is about how to get the last element of the list in Python as shown below.
If you want to learn about Python Lists, visit Python List Tutorials.
Example 1: Input list: [87,21,76,32,19,44] Output: Last element in a list is 44 Example 2: Input list: [21,45,87,32] Output: Last element in a list is 32
Let’s discuss them one by one.
USING LEN() to print last item of a list in python
The most basic method is to find the length of the list using len() function. Suppose you have a list a=[12, 71, 23, 44, 33]. The length of list a is 5 and the last index is in the fourth position. Therefore, the total length – 1 gives the last index of the list.
Example 1:
a = [12, 71, 23, 44, 33]
index=len(a)-1
print("Last Element: ", a[index])
Output:
Last Element: 33
use Negative indexing to get the last item a list in python
In python, indexing allows us to access an element from the list. Suppose you have a list a= [12, 13, 65, 21, 32]. You can access any item through its index such as a[0], a[1], a[2] gives you 12, 13, and 65 as an output. In this case, a[4] gives you the last element. But if you don’t know the length of a list then this method fails. Python supports negative indexing which starts from the last element of the list that is a[-1] will return 32 as an output. Similarly, a[-2], a[-3] and a[-4] return 21, 65 and 13 as an output.
Example 2:
#create and initialize a list
data=[87, 21, 76, 32, 19, 44]
#print the last element of a list
print("The last element of list is: ", data[-1])
Output:
The last element of list is: 44
If the dictionary is empty, then the above program will raise an index error.
get last item of list using pop() method
The list has a pop() function which accepts the index as an optional argument. This command returns and removes the item at the given index position. The default value of the index is -1 if the pop function is called without specifying any value as an argument. The command on execution removes and returns the last item of the list.
Example 3:
data = ["John", "David", "Susan", "Cabria"]
last_item=data.pop()
print("Last Element: ",last_item)
Output:
Last Element: Cabria
These methods are used to get the last element from the list. But, if you are asked to find the last n elements from the list, then you can do this by list slicing.
Example 4:
# create an empty list
list1 = []
# Take number of elements as input from user
length = int(input("Enter number of elements : "))
# initialize the list using for loop
for i in range(0, length):
item = int(input("Enter element "+str(i+1)+" :"))
list1.append(item)
index = int(input("Enter the number of last elements you want to display: "))
#print last N elements
result = list1[-index:]
# print result
print("The last N elements of list are : ",result)
Output:
Enter number of elements : 6
Enter element 1 :1
Enter element 2 :5
Enter element 3 :23
Enter element 4 :13
Enter element 5 :44
Enter element 6 :39
Enter the number of last elements you want to display: 3
The last N elements of list are : [13, 44, 39]
In this tutorial, we have learned different methods by which we can access and print the last elements or last n elements from the list. You can also use list.pop() method to print n elements. For this, you need to use for loop. Try this on your own and let us know if you have any queries related to this topic. Contact us.