Open In App

Get the Last Element of List in Python

Last Updated : 10 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn about different ways of getting the last element of a list in Python.

For example, consider a list:

Input: list = [1, 3, 34, 12, 6]
Output: 6

Explanation: Last element of the list l in the above example is 6.

Let's explore various methods of doing it in Python:

1. Using Negative Indexing

The simplest and most efficient method uses negative indexing with a[-1]. In Python, we can use -1 as an index to access the last element directly.

Python
a = [1, 2, 3, 4, 5]

print(a[-1])

Output
5

2. Using len() with Indexing

We can also find the last element by using len() function. Find length of the list and then subtracting one to get the index of the last element.

Python
a = [1, 2, 3, 4, 5]

le = a[len(a) - 1]  

print(le)

Output
5

Explanation:len(a) - 1 gives the index of the last item, which is then used to retrieve the value.

3. Using len() with Indexing

We can also find the last element by using len() function. Find length of the list and then subtracting one to get the index of the last element.

Python
a = [1, 2, 3, 4, 5]

le = a[len(a) - 1]  

print(le)

Output
5

Explanation:len(a) - 1 gives the index of the last item, which is then used to retrieve the value.

4. Using Slicing

Another interesting way to get the last element is by using slicing. If we slice the list with -1:, it returns a list with just the last element.

Python
a = [1, 2, 3, 4, 5]

le = a[-1:] 

print(le)

Output
[5]

Explanation:

  • a[-1:] creates a new list with just the last element.
  • It’s less efficient than direct indexing because it involves memory allocation for the new list.

5. Using pop() Method

Another way to get the last element is by using pop() method. This method removes and returns the last element of the list.

Python
a = [1, 2, 3, 4, 5]

le = a.pop()  

print(le)

Output
5

Explanation:

  • pop() removes the last element from the list and returns it.
  • a becomes [1, 2, 3, 4] after the operation, and le stores the removed element 5.

Note: If you only want to read the value and not change the list, this method is not ideal.

Related articles:


Similar Reads