Get Last Element of a List in Python Example
Oct 19, 2022
Hi Dev,
In this example, I will show you Get Last Element of a List in Python Example. I explained simply about Python Get Last Element of List. This article will give you simple example of How to Get Last n Elements of a List in Python. We will use How to Find Last Element of List in Python.
There are a few ways to get the last element from the list in python. You could use either by key with -1 or using pop function. so let's see the below examples:
so let's see following examples with output:
Example 1:
main.py
myList = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
# Get Last Element
lastElem = myList[-1]
print(lastElem)
Output:
Sat
Example 2:
main.py
myList = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
# Get Last Element
lastElem = myList.pop()
print(lastElem)
Output:
2
- Get Last 2 Digits of Number in Python Tutorial Example
- How to Sum of First and Last Digit of Number in Python?
- How to Get First and Last Digit of Number in Python?
- Get the Last Digit of Number in Python Tutorial Example
- Get the First Digit of Number in Python Tutorial Example
- How to Remove All Decimals from Number in Python?
- How to Convert String to Float with 2 Decimal Places in Python?
- How to Format Number to 2 Decimal Places in Python?