negative indices for sequence types

Jacek Generowicz jacek.generowicz at cern.ch
Wed Sep 17 04:43:13 EDT 2003


danbmil99 at yahoo.com (dan) hypothesizes:

> My explanation of the behavior is correct however.  list[a] always
> equals list[a % len(list)].  A negative number mod N = its absolute
> value subtracted from N:

Proof by counterexample:

Python 2.2.2 (#1, Feb  8 2003, 12:11:31) 
[GCC 3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = '0123'
>>> s[-20 % len(s)]
'0'
>>> s[-20]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: string index out of range


Your explanation of the behaviour is incorrect.

QED.




More information about the Python-list mailing list