53

I am able to get the second-to-last element of a list with the following:

>>> lst = ['a', 'b', 'c', 'd', 'e', 'f']
>>> print(lst[len(lst)-2])
e

Is there a better way than using print(lst[len(lst)-2]) to achieve this same result?

PyNoob
  • 1,136
  • 1
  • 8
  • 22

1 Answers1

97

There is: negative indices:

lst[-2]
Scott Hunter
  • 46,905
  • 10
  • 55
  • 92
  • 3
    Even with the knowledge of `-1` I didn't think to subtract more... thank you! I will accept when the time limit is over. – PyNoob Oct 03 '16 at 19:22