ItSolutionStuff.com

How to Get the Last Element of a List in Python?

By Hardik Savani โ€ข October 30, 2023
Python

Hi,

If you need to see an example of how to get last element of list in python. Iรขโ‚ฌโ„ขm going to show you python get last element of list. This tutorial will give you a simple example of how to find last element of list in python. you can understand the concept of how to get last n elements of a 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:

You can use these examples with python3 (Python 3) version.

let's see below a simple example 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

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

๐Ÿ“บ Subscribe on YouTube

We Are Recommending You

โ˜…

How to Remove Last Element from List in Python?

Read Now โ†’
โ˜…

How to Remove First Element from List in Python?

Read Now โ†’
โ˜…

Python List Remove Element by Value Example

Read Now โ†’
โ˜…

Python List Remove Element by Index Example

Read Now โ†’
โ˜…

How to Get All Files from Directory in Python?

Read Now โ†’
โ˜…

Python Delete Files Matching Pattern Example

Read Now โ†’
โ˜…

Python Delete Folder and Files Recursively Example

Read Now โ†’
โ˜…

Python Delete Directory if Exists Example

Read Now โ†’
โ˜…

Python Post Request with Basic Authentication Example

Read Now โ†’
โ˜…

Python Post Request with Bearer Token Example

Read Now โ†’
โ˜…

Python Get File Extension from Filename Example

Read Now โ†’
โ˜…

How to Compare Two Dates in Python?

Read Now โ†’