Get the Last Element in a List in Python

  • Post category:List

Accessing the last element of a list is a common operation in Python programming. In this blog post, we’ll explore different code examples that demonstrate how to retrieve the last element of a list efficiently.

Example 1:

fruits = ['apple', 'banana', 'orange', 'kiwi']
last_fruit = fruits[-1]
print(last_fruit)

Output: “kiwi”

Example 2:

numbers = [1, 2, 3, 4, 5]
last_number = numbers[len(numbers) - 1]
print(last_number)

Output: 5