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

Spread the love

Problem –

You want to get the last element of the list in python.

Solution –

To get the last element of a list, you can use negative indexing. The negative indexing starts from -1 from the end.

In [1]: lst = [2, 4, 6, 8, 10]

In [2]: # get the last element from the list

In [3]: lst[-1]
Out[3]: 10

In [4]: # get the last 3 elements from the list

In [5]: lst[-3:]
Out[5]: [6, 8, 10]

Rating: 1 out of 5.

Leave a Reply