59

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?

Stephen B
  • 1,246
  • 1
  • 10
  • 23

1 Answers1

106

There is: negative indices:

lst[-2]
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • 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. – Stephen B Oct 03 '16 at 19:22