Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get the last element of a list in Python?
In Python, getting the last element of a list is a common operation. There are several approaches to accomplish this task, each with different use cases and behaviors ?
Using Negative Indexing
Python supports negative indexing, where -1 refers to the last element, -2 to the second-to-last, and so on. This is the most common and pythonic way.
Example
The following program returns the last element using negative indexing ?
# input list
numbers = [5, 1, 6, 8, 3]
# printing input list
print("Input list:", numbers)
# getting the last element using len(list)-1 as index
print("Last element using len(list)-1:", numbers[len(numbers) - 1])
# getting the last element using -1 (negative indexing)
print("Last element using -1 index:", numbers[-1])
Input list: [5, 1, 6, 8, 3] Last element using len(list)-1: 3 Last element using -1 index: 3
Using Slicing
List slicing with [-1:] returns a new list containing only the last element. To get the element itself, access index [0] of the result.
Example
The following program returns the last element using slicing ?
# input list
numbers = [5, 1, 6, 8, 3]
# printing input list
print("Input list:", numbers)
# getting the last element using slicing
last_element = numbers[-1:][0]
# printing the last element
print("Last element using slicing:", last_element)
Input list: [5, 1, 6, 8, 3] Last element using slicing: 3
Using pop() Method
Warning: The pop() method removes and returns the last element, modifying the original list.
Example
The following program returns the last element using the pop() method ?
# input list
numbers = [5, 1, 6, 8, 3]
# printing input list
print("Input list:", numbers)
# getting and removing the last element using pop()
last_element = numbers.pop()
print("Last element using pop():", last_element)
print("List after pop():", numbers)
Input list: [5, 1, 6, 8, 3] Last element using pop(): 3 List after pop(): [5, 1, 6, 8]
Using For Loop
This approach iterates through the list and identifies the last element by checking if the current index equals len(list) - 1.
Example
The following program returns the last element using a for loop ?
# input list
numbers = [5, 1, 6, 8, 3]
# printing input list
print("Input list:", numbers)
# traversing the list to find the last element
for i in range(len(numbers)):
if i == (len(numbers) - 1):
print("Last element using for loop:", numbers[i])
Input list: [5, 1, 6, 8, 3] Last element using for loop: 3
Comparison
| Method | Modifies List? | Performance | Best For |
|---|---|---|---|
list[-1] |
No | O(1) | General use (recommended) |
list[-1:][0] |
No | O(1) | When you need a slice |
list.pop() |
Yes | O(1) | When you need to remove the element |
| For loop | No | O(n) | Learning purposes only |
Conclusion
Use list[-1] for getting the last element as it's the most pythonic and efficient approach. Use pop() only when you need to remove the element, and avoid the for loop method in production code due to poor performance.
