simple beginner question about lists and negative index

J

jmDesktop

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.
 
J

jmDesktop

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.
 
T

Terry Reedy

| 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.

A sequence with n items has n+1 slice positions, numbered 0 to n: the 2 at
beginning and end and n-1 between items. Example
-a-b-c-
0 1 2 3
has 4 slice positions.
Hence the first item is seq[0:1] and last is seq[n-1:n]

In a sense, we 'ought' to index sequences with average of two successive
slice positions, giving seq[1/2],,,seq[n-1/2].
But this is inconvenient, so we either round down (C, Python, etc) or up
(Fortran), giving seq[0],,,,seq[n-1] or seq[1],,,seq[n].
Python allows n-1 and n-k to be abbreviated as -1 and -k.

-1 as an abbreviation of n-1 is only the end of the list for indexing.
n is the end for slicing. It is abbreviated by omission. Perhaps

for i in range(n+1): print i, 'abcde'[:5-i]

will make this all even clearer.

Terry Jan Reedy
 
D

dwblas

|
| s = 'abcde'
| i = -1
| for i in range (-1, -len(s), -1):
| print s[:i], i
| Why doesn't the first one have the e if -1 is the end of the list?

That should be obvious. 'print s[-1]' will yield the "e", but your
code is using slicing, s[:i], not printing elements from a list. Your
code starts with the value s[:-1], which is exactly the same as
s[0:-1], so you print the string from 0 to -1 which omits the last
letter. The next time it omits the last 2 letters s[:-2]. As already
stated, that should be very obvious. Also, if you want a response in
the future, make your code readable. That means not using, "i", "l",
or "o" as variable names. They look too much like numbers and some
people won't waste their time trying to distinguish between them.
There are 23 other letters that work just a well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top