About this question
This question explores simple and effective ways to access the final item in a list, including using negative indexing and built-in functions for safe and readable code.
This question explores simple and effective ways to access the final item in a list, including using negative indexing and built-in functions for safe and readable code.
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Python Expert
Answered on Jun 23, 2025
Getting the last element of a list in Python is super easy and one of the most common tasks you’ll encounter. Python offers a few simple ways to do this, depending on your needs.
1. Using Negative Indexing
Python supports negative indexing, which lets you count from the end of the list:
my_list = [10, 20, 30, 40]
last_item = my_list[-1]
print(last_item) # Output: 40 2. Using len() Function (Less common)
You can also use the len() function to get the last item manually:
last_item = my_list[len(my_list) - 1]This is more verbose and rarely needed, but useful if you're doing calculations with indices.
3. Using Slicing (Returns a list)
If you want the last n elements, slicing is your friend:
last_element = my_list[-1:] # Returns [40] Be Careful with Empty Lists
If the list is empty, accessing my_list[-1] will throw an IndexError. You can avoid that with a check:
if my_list:
print(my_list[-1])
else:
print("List is empty")Schmidt Raven Latest answer
Answered on Aug 13, 2026
If you only want to peek at the last element without modifying the list, negative indexing (my_list[-1]) is still the best and most Pythonic choice free online games. Just remember to always check if the list is empty first to avoid an IndexError.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common Python interview questions, answered
Guides, tips and career advice on Python from JanBask experts.
Python How to Become a Python Developer: A Step-by-Step Guide for Beginners
Discover how to become a Python Developer via a practical 7-step guide build projects, maintain clean code, practice daily, and…
Python PCEP Certification Guide: Entry-Level Python Programmer Certification
Explore this PCEP certification guide for insights on cost, exam format, passing score, application, training, and study tips.…
Python Top 50+ Python Projects ideas for Beginners to Advanced
Discover innovative Python project ideas, explore advanced Python projects, and find good Python projects for beginners and…
Python How To Comment Out Multiple Lines In Python Like A Pro
Commenting out code is a common practice among programmers. It allows you to temporarily disable parts of your code without…