How to get the second-to-last element of a list in Python?


Python sequence, including list object allows indexing. Any element in list can be accessed using zero based index. If index is a negative number, count of index starts from end.  As we want second to last element in list, use -2 as index.

>>> L1=[1,2,3,4,5]
>>> print (L1[-2])
4


Updated on: 20-Feb-2020

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements