bananasushi
bananasushi

Reputation: 1

Stepping backwards with lists

In 'Head First Python' on page 79, they show an example of stepping in reverse with a list and joining it but they don't explain it.

I've tried to troubleshoot it by trying different numbers to isolate what each number means.

Anyways, first the book's code:

>>>book = "The Hitchhiker's Guide to the Galaxy"

>>>booklist = list(book)
['T', 'h', 'e', ' ', 'H', 'i', 't', 'c', 'h', 'h', 'i', 'k', 'e', 'r', "'", 's', ' ', 'G', 'u', 'i', 'd', 'e', ' ', 't', 'o', ' ', 't', 'h', 'e', ' ', 'G', 'a', 'l', 'a', 'x', 'y']

>>> ''.join(booklist)
"The Hitchhiker's Guide to the Galaxy"
>>> ''.join(booklist[4:14])
'Hitchhiker'
>>> ''.join(booklist[13:3:-1])
'rekihhctiH'

Why 13:3 this time instead of reversing the 4:14 to 14:4.

I tried this to isolate what values the numbers in start, stop represent but it doesn't make any sense

>>> ''.join(booklist[35:0:-1])
"yxalaG eht ot ediuG s'rekihhctiH eh"

What Happened to the "T"? It won't work if I put a -1 in.

In going the forward way with 4:14, I figured "H" is the 4th numeral from the beginning by going 0, 1, 2, 3, 4. But "r" if I count that way, I get 13. Why isn't it consistent?

In going the backwards way with 13:3, I can get "r" as 13 from counting from "0" but how do you get 'H' from '3'? No matter if you count from '0' or '1', the 'H' would be 4 or 5 respectively. But it seems to work out that way because a letter (the 'T') is lost.

Furturmore, in trying to figure out what's what in ranges, lists etc. I consulted another book "Python Crash Course" page 65. Quote, "As with the range() function, Python stops one item before the second index you specify"

players = ['1', '2', '3', '4', '5']
print(players[0:3])
['1', '2', '3']

the first digit '0' is the first item in the list, but the second '3' IS the third item in the list.

but on page 61, they say the following,

for value in range(1, 5)
    print(value)
1
2
3
4

That's not the same thing.

In the list players[0:3], the 0 is 1 and 3 is 3

In the range(1, 5), 1 is 1 and 5 is 4

That's backwards! And why in the list, aren't they both counting from '0'? This is so complicated and confusing you would think these "highly regarded" beginner's books that I have cross referenced would spend more time on such a fundamental principle. And how can "Head First Python" provide that reverse slice. That is chapter 2. There's no way any beginner will understand that.

Upvotes: 0

Views: 79

Answers (1)

drum
drum

Reputation: 5651

Each element of a list has an index, starting from 0.

In list slicing:

[a:b:c]

a - initial index (inclusive)

b - last index (exclusive)

c - the steps (can think of like skipping. if this is negative, the go from the back of the list)

For [4:14], it goes from index 4 to index 13 (14 is where it stops) which gets you Hitchhiker

For [13:3:-1], because of -1 it goes backwards from index 13 to index 4 (again, 3 is where it stops)

For [35:0:-1], again reversed, it goes from index 35 to index 1.

Recommended reading: https://docs.python.org/3/tutorial/introduction.html

Upvotes: 3

Related Questions