0

I was wondering why negative indices in python are so important - are there any applications? I could just use

my_list[len(my_list) - 1]

to find the last element of my_list. Why do we need

my_list[-1]

To find the last element?

EDIT: It's not that I don't understand what my_list[-1] means, I want to know where this might be used.

SnakeException
  • 1,148
  • 1
  • 8
  • 26
  • Because it's simpler, and simple is important. To get the element at index `n`, you could also write `my_list[len(my_list) - (len(my_list) - n)]`. But we don't do that because it's more complex than `my_list[n]`. – jfaccioni May 29 '19 at 01:46
  • Where would it be used? Also I get it is simpler, but I imagine it is hard to live with if somebody decides to learn python... – SnakeException May 29 '19 at 01:47
  • This is a different question than what you posed originally. You wrote: "I could just use `my_list[len(my_list) - 1]` to find the last element of my_list. Why do we need `my_list[-1]` To find the last element?" The use cases of `my_list[len(my_list) - 1]` are the same as `my_list[-1]`, it's just that the latter is simpler than the former. – jfaccioni May 29 '19 at 01:51
  • That part was an example. – SnakeException May 29 '19 at 02:02

0 Answers0