04-Nov-2022
.
Admin
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
#Python