In this tutorial, I will explain various methods to get the last element of a list in Python. As a developer working on a project for one of my clients, I came across a scenario where I needed to get the last element of the list. Let us explore different approaches and provide practical examples and screenshots.
Python Lists
Before we get into retrieving the last element, let’s briefly discuss Python lists. In Python, a list is an ordered, mutable, and versatile data structure that allows you to store and manipulate a collection of elements. Lists are defined using square brackets [] and can contain elements of different data types.
Here’s an example of a list containing names of some major cities in the USA:
cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]Read How to Split a List in Python?
Get the Last Element of a List in Python
In Python, you can access individual elements of a list using indexing. The index starts from 0 for the first element and increments by 1 for each subsequent element. You can also use negative indexing to access elements from the end of the list, where -1 represents the last element, -2 represents the second-to-last element, and so on.
For example, to access the first and last elements of the cities list:
first_city = cities[0] # "New York"
last_city = cities[-1] # "Phoenix"Method 1: Use Negative Indexing
The simplest and most common way to get the last element of a Python list is by using negative indexing. By specifying an index of -1, you can directly access the last element.
Consider the following example:
states = ["California", "Texas", "Florida", "New York", "Illinois"]
last_state = states[-1]
print("The last state in the list is:", last_state)Output:
The last state in the list is: IllinoisYou can refer to the below screenshot to see the output.

In this example, we have a list of states in the USA. By using states[-1] we retrieve the last element of the list, which is “Illinois”.
Check out How to Print Lists in Python?
Method 2: Use the len() Function
Another approach to get the last element is by using the len() function in combination with indexing. Python len() function returns the length of the list, and by subtracting 1 from it, you can obtain the index of the last element.
Here’s an example:
presidents = ["George Washington", "John Adams", "Thomas Jefferson", "James Madison", "James Monroe"]
last_index = len(presidents) - 1
last_president = presidents[last_index]
print("The last president in the list is:", last_president)Output:
The last president in the list is: James MonroeYou can refer to the below screenshot to see the output.

In this example, we have a list of US presidents. We calculate the index of the last element by subtracting 1 from the length of the list using len(presidents) - 1. Then, we use that index to access the last element and store it in the last_president variable.
Read How to Divide Each Element in a List by a Number in Python?
Method 3: Use the pop() Method
The pop() method in Python allows you to remove and return an element from a list at a specified index. By default, if no index is provided, pop() removes and returns the last element of the list.
Consider the following example:
tech_companies = ["Apple", "Google", "Microsoft", "Amazon", "Facebook"]
last_company = tech_companies.pop()
print("The last company in the list is:", last_company)
print("Updated list:", tech_companies)Output:
The last company in the list is: Facebook
Updated list: ['Apple', 'Google', 'Microsoft', 'Amazon']You can refer to the below screenshot to see the output.

In this example, we have a list of tech companies in the USA. By calling tech_companies.pop() Without any arguments, we remove and return the last element of the list, which is “Facebook”. The original list is modified, and the last element is removed.
Check out How to Find the Closest Value in a List Using Python?
Method 4: Use List Slicing
List slicing is a powerful feature in Python that allows you to extract a portion of a list. You can use list slicing to retrieve the last element by specifying a slice that starts from the last index and goes up to the end of the list.
Here’s an example:
rivers = ["Mississippi", "Missouri", "Colorado", "Ohio", "Columbia"]
last_river = rivers[-1:]
print("The last river in the list is:", last_river[0])Output:
The last river in the list is: ColumbiaIn this example, we have a list of major rivers in the USA. By using the slice rivers[-1:], we extract a sublist that starts from the last element and goes up to the end of the list. The resulting sublist contains only one element, which is the last element of the original list. We access that element using index 0.
Check out How to Sort a List in Python Without Using the sort() Function?
Handle Empty Lists
When working with lists, it’s important to consider the case of empty lists. Attempting to access the last element of an empty list will raise an IndexError. To handle such cases, you can use a conditional statement to check if the list is empty before accessing the last element.
Here’s an example:
empty_list = []
if empty_list:
last_element = empty_list[-1]
print("The last element is:", last_element)
else:
print("The list is empty.")Output:
The list is empty.In this example, we have an empty list called empty_list. We use a conditional statement to check if the list is empty. If the list is not empty, we proceed to access the last element using empty_list[-1]. However, if the list is empty, we print a message indicating that the list is empty.
Read How to Check if Any Element in a List is Present in Another List using Python?
Conclusion
In this tutorial, I explained various methods to get the last element of a list in Python. I discussed using negative indexing using the len() function, the pop() method, and list slicing.I also explained how to handle empty lists.
You may like to read:
- How to Split a Python List Into Evenly Sized Chunks?
- How to Find the Index of the Maximum Value in a List using Python?
- How to Capitalize the First Letter of Every Word in a List using Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.