In this Python tutorial, we will see how to get the last element in the Python list with different methods using demonstrative examples.
A list in Python is an ordered sequence of items that can contain a mix of different data types, including numbers, strings, and other lists. Lists are mutable, meaning we can change their elements. They are defined by enclosing a comma-separated sequence of elements within square brackets [].
Let’s take an example, a list of the top five tallest buildings in the USA:
buildings = ['One World Trade Center', 'Central Park Tower', 'Willis Tower', 'One Vanderbilt', '111 West 57th Street']
Now, if we have to take out the last element of the Python list i.e., ‘111 West 57th Street’. We can use many different ways to do so.
Getting the last element in the Python list
There are several different ways to get the last element in the Python list. They are:
- Negative Indexing
- len() Function
- the pop() Function
- use list slicing
- Reverse Iterator
We will see all these methods in detail with examples.
Method-1: Using Negative indexing to get the last element of the Python list
Python list supports negative indexing, where -1 refers to the last item, -2 to the second last, and so forth. So, we can fetch the last item in our list like so:
Consider a list of the top five most populated states in the USA. The last state in our list is ‘Pennsylvania’. By using negative indexing, we can retrieve it:
states = ['California', 'Texas', 'Florida', 'New York', 'Pennsylvania']
last_state = states[-1]
print(last_state)
The output is:
Pennsylvania
This way we can use negative indexing to get the last element in the Python list.
Method-2: Getting the last element of the Python list using the Len() function
The built-in len() function in Python list returns the number of items in a list. Since indices start at 0, the last item’s index will be one less than the length of the list. We can fetch the last item like this:
Consider a list of some of the longest rivers in the USA. Here, ‘Arkansas’ is the last river in our Python list. We can retrieve it using the len() function:
rivers = ['Missouri', 'Mississippi', 'Yukon', 'Rio Grande', 'Arkansas']
last_river = rivers[len(rivers) - 1]
print('The last river in the List is:',last_river)
The output is:
The last river in the List is: Arkansas
This way we can use the len() function to get the last element of the Python list.
Method-3: Getting the last element of the list using the pop() function
Python’s pop() function removes the item at a specific position in a list and returns it. When called without an argument, it defaults to the last item in the list.
let’s consider a Python list of some of the major airlines in the USA. The last airline in our Python list is ‘Alaska’. We can retrieve it using the pop() function:
airlines = ['Delta', 'American', 'United', 'Southwest', 'Alaska']
last_airline = airlines.pop()
print('The last airline in the list is: ',last_airline)
The output is:
The last airline in the list is: Alaska
Note: This method will remove the last element from the list.
This way we can use the pop() function to remove the last element of the Python list.
Method-4: Getting the last element of the Python list using List slicing
List slicing is another efficient way to access elements within a Python list, including the last element.
Consider a list of the top five most visited National Parks in the USA. The last park in our Python list is ‘Yosemite’. We can use list slicing to retrieve it:
national_parks = ['Great Smoky Mountains', 'Yellowstone', 'Zion', 'Rocky Mountain', 'Yosemite']
last_park = national_parks[-1:]
print('The last park name in the list is: ', last_park)
The output is: This method will give us a Python list with the last element in it.
The last park name in the list is: ['Yosemite']
This way we can use list slicing to get the last element from the list in Python
Method-5: Using for loop to get the last element of the list in Python
To get the last element of the Python list using for loop with conditional statements is the naive method in Python.
Consider a list of the top five most popular e-commerce websites in the USA:
ecommerce_websites = ['Amazon', 'eBay', 'Walmart', 'Etsy', 'Home Depot']
for i in range(0, len(ecommerce_websites)):
if i == (len(ecommerce_websites) - 1):
print("The last website of list using loop : "
+ str(ecommerce_websites[i]))
The output is:
The last website of list using loop : Home Depot
This way we can use the for loop to get the last element of the list in Python.
Conclusion:
Python’s flexibility offers several efficient ways to get the last element of a list. In this article, we’ve explored negative indexing, the len() function, the pop() method, list slicing, and the for loop. Each approach comes with its strengths and use cases. The choice of method largely depends on our specific needs and the problem.
You may also like to read :
- How to remove the first element from a list in Python
- How to add a string to a list in Python
- How to replace items in a Python list
- How to remove the last element from the Python list
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.