

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python How to get the last element of list
In this tutorial, we are going to see different ways to get the last element from the list. Let's see one by one.
Index
We can get the last element of the list using the index. And we can get the index of the last element by the length of the list. Let's see the code.
Example
# initializing the list numbers = [1, 2, 3, 4, 5] # printing the last element print(f"Last element:- {numbers[len(numbers) - 1]}")
Output
If you execute the above program, then you will get the following result.
Last element:- 5
Negative Index
We can get the last element of the list using negative indexing. And the negative indexing from -1. Let's see the code.
Example
# initializing the list numbers = [1, 2, 3, 4, 5] # printing the last element print(f"Last element:- {numbers[-1]}")
Output
If you execute the above program, then you will get the following result.
Last element:- 5
Pop
We can get the last element of the list by popping it out. The pop method of the list removes returns the last element from the list. Let's the code.
Example
# initializing the list numbers = [1, 2, 3, 4, 5] # printing the last element print(f"Last element:- {numbers.pop()}")
Output
If you execute the above program, then you will get the following result.
Last element:- 5
Conclusion
If you have any doubts regarding the tutorial, mention them in the comment section.
- Related Questions & Answers
- How to get the last element of a list in Python?
- How to get the second-to-last element of a list in Python?
- Get last element of each sublist in Python
- How to get the last element in JavaScript array?
- Last occurrence of some element in a list in Python
- Find the last element of a list in scala
- How to get last element in android ConcurrentLinkedDeque?
- Python program to get the indices of each element of one list in another list
- Get first and last elements of a list in Python
- How to find the last occurrence of an element in a Java List?
- Get the element ordered last in Java TreeSet
- Get last N elements from given list in Python
- C# program to get the last element from an array
- How to use $slice operator to get last element of array in MongoDB?
- How to get last day of a month in Python?