Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Python negative indexing isn't modulo. It's just shorthand for array[len(array)-i].

  >>> a = [1, 2, 3]
  >>> a[-1]
  3
  >>> a[-2]
  2
  >>> a[-3]
  1
  >>> a[-4]
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  IndexError: list index out of range


The problem is that you need to have a conditional to figure out if I is negative or not. With modulo it’s all the same.




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: