Skip to Content

3 Ways to Get Last Element of a List In Python

Python is a versatile language that can be used for all sorts of tasks. In this blog post, we will discuss how to get the last element in a list using three different methods. Each method has its own advantages and disadvantages, so it’s important to understand them all before deciding which one to use in your own code. Let’s get started!

Understanding Python list

A list is a data structure in Python that is a mutable, ordered sequence of elements. Mutable means that you can change the items inside, while the ordered sequence is in reference to index location.

The first element in a list will always be located at index 0. Each element or value that is inside of a list is called an item. Lists are defined in Python by enclosing a comma-separated sequence of objects in square brackets ([]). So, if we want to get the last element in a list, we can just use index -1.

Methods to get the last element of a list in Python

There are 3 ways to get the last element of a list in Python.

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!
  • Using list[-1]: last_element=list[-1]
  • Using list.pop() method: last_element=list.pop()
  • Using list indexing: last_element=list[len(list) – 1]

 

Let’s see these approaches one by one.

Get the last element of a list using list[-1] in Python

The most effective way to get the last element of a list in Python is using the list[-1]. Python allows you to use negative indices, which count from the end of the list instead of the beginning. So list[-1] gets the last element, list[-2] gets the second to last. The list[-1] is the most preferable, shortest, and Pythonic way to get the last element.

To create a list in Python, use the square brackets and add the items in the list separated by commas.

>>> data = []
>>> for i in range(10):
… data.append(i)

or
data = list(range(0, 10))

>>> data
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In this list, the 9 element is the last item, and to access that element, write the following code.
last_element = data[-1]

What this code will do is that it will pluck the last element and store it into the last_element, and we can print the element using the print() function.

last_element = data[-1]
print(last_element)
Output
9

 

Get the last element of a list using pop Method in Python

The list.pop() in the Python method removes and returns the object at the given index (passed as an argument) from a list.

data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
last_element = data.pop()
print(last_element)
Output
9

Now data=[0, 1, 2, 3, 4, 5, 6, 7, 8]

Get the last element of a list using indexing in Python

As the indexing in a list starts from 0th index. So, if our list length is S, then we can get the last element of a list by selecting item at index position S-1.

Let’s understand this by an example.
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# get element at index position size-1
last_elem = data[len(data) – 1]
print(‘Last Element: ‘, last_elem)

IndexError in Python List

If the expected item does not exist, then it will throw an IndexError. This means that list[-1] will raise an exception if the list is empty because an empty list can’t have the last element.

data = []
last_element = data[-1]
print(last_element)
Output
Traceback (most recent call last):
File "/Users/krunal/Desktop/code/pyt/database/app.py", line 3, in <module>
last_element = data[-1]
IndexError: list index out of range

As we can see that we got: IndexError: list index out of range

Related:

 

Hux Gen

Wednesday 22nd of November 2023

It works for me. Thank you.

三种方法获取Python 列表最后一个元素 | 编程案例分享

Tuesday 4th of October 2022

[…] 参考:how to get last element of a list in python […]