simple beginner question about lists and negative index

jmDesktop needin4mation at gmail.com
Thu May 1 11:03:21 EDT 2008


On May 1, 10:59 am, jmDesktop <needin4mat... at gmail.com> wrote:
> This program:
>
> s = 'abcde'
> i = -1
> for i in range (-1, -len(s), -1):
>     print s[:i], i
>
> gives
>
> abcd -1
> abc -2
> ab -3
> a -4
>
> Why doesn't the first one have the e if -1 is the end of the list?  In
> Dive Into Python it said that -1 was the end of the list.  Thanks.
>
> it is from Chun's book, slightly modified by me to see the index.

Sorry.  It's because of the : right?  up to but not including.  Dive
into was just list[-1] not list[:-1].  I looked at the for while
before posting and then as soon as I posted I saw it.



More information about the Python-list mailing list