Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Get the Last Element of a List in Python

  • 19 Jul, 2025
  • Com 2
How to get the last element in the Python List

The most recommended and Pythonic way to retrieve the last element of a list is to use negative indexing (list[-1]), provided the list is not empty.

Getting the Last Element of a List in Python

main_list = [1, 2, 3, 4, 5]

last_element = main_list[-1]

print("The last element of the list is:", last_element)

# Output: The last element of the list is: 5

If you start indexing backwards, the last index of the list will be -1, which is what we need to access the element. The -2 index refers to the second-to-last element, and so on.

This is the most efficient approach because it has O(1) time complexity.

What if the list is empty?

What if the list is empty?

If the list is empty and you use negative indexing, it will throw the IndexError: list index out of range exception.

empty_list = []

last_element = empty_list[-1]

print("The last element of the list is:", last_element)

# Output: IndexError: list index out of range

To prevent this type of error, you can wrap it in a check to ensure the list is not empty. It is a fail-safe approach.

main_list = []

if main_list:
    last_element = main_list[-1]
    print("The last element of the list is:", last_element)
else:
    print("List is empty")

# Output: List is empty

List with a single element

List with a single element

If the input list has only one element, it will return that element because it becomes the last element.

main_list = [21]

if main_list:
    last_element = main_list[-1]
    print("The last element of the list is:", last_element)
else:
    print("List is empty")

# Output: The last element of the list is: 21

Since there is only 21, it returns 21 as output.

Alternate approaches

Here are three alternate ways:

  1. Using list[len(list)-1]
  2. Using pop()
  3. Using slicing

Using list[len(list)-1]

Using list[len(list)-1]

Python has a built-in len() function that you can use to calculate the total length of the list and exactly pinpoint the last element.

main_list = [11, 21, 19, 48, 18]

last_element = main_list[len(main_list)-1]

print(last_element)

# Output: 18

It explicitly uses the length to find the last position, but less concise than negative indexing.

If the list is empty, it will again raise the IndexError.

Using a list.pop()

Using list.pop() to remove the last element

 

The list.pop() method removes and returns the last element from the list.

Avoid using this method if you wish to retain all elements, as it permanently removes the last element.

main_list = [1, 2, 3, 4, 5]

last_element = main_list.pop()

print("The last element of the list is:", last_element)

# Output: The last element of the list is: 5

Using slicing

Using slicing to delete the last element of the list

 

By using a slice [-1:], you get the last element in a new list.

Since the output is a list, you can access the element itself using [0].

main_list = [1, 2, 3, 4, 5]

last_element = main_list[-1:][0]

print("The last element of the list is:", last_element)

# Output: The last element of the list is: 5

That’s all!

Post Views: 23
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Python List append(): Adding an Element to a List
How to Find the First Non-NaN Value in a Python List

2 Comments

  1. proGrammer

    August 2, 2022 at 6:56 pm

    what is I want to get last second elemnt of a list?

    Reply
    1. Krunal Lathiya

      July 24, 2025 at 12:16 pm

      To retrieve the second-to-last element, simply pass an index of -2.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend