Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Reversing the order via negative int

I don't seem to understand the concept of reversing the order via negative int numbers = list(range(21)) print(numbers[2::2]) gives me: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] but why doesnt print(numbers[2::-1]) give me the reverse of gives me: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] but rather [2, 1, 0]

Can someone explain please?

2 Answers

Hi Allen,

The parameters in a slice are:

numbers[start:stop:step]

In your first example, numbers[2::2], you are telling it to start at index 2 (which happens to be the number 2), and stop at the end (since there is no 'stop' parameter given), and to increment by 2.

In your second example, numbers[2::-1], you are again telling it to start at index 2, and to decrement by 1 until you reach the end (which will be the beginning in this case because you are stepping in reverse). This is why you end up with [2, 1, 0].

If you wanted to reverse the whole list so it is the exact opposite of your first example, you'd need to do the following:

>>> numbers[-1:1:-2]
[20, 18, 16, 14, 12, 10, 8, 6, 4, 2]

Here we start at the very end (-1), we want it to finish at index 1 (so we don't miss index 2), then we step backwards by -2.

Hope this clears things up.

"In your second example, numbers[2::-1], you are again telling it to start at index 2, and to decrement by 1 until you reach the end (which will be the beginning in this case because you are stepping in reverse). This is why you end up with [2, 1, 0]." I understand this logic, by why does numbers[::-1] give the reverse of numbers[:] rather than [] because woudnt it start from 0 to the end so if decrement by -1 it just be []?

Oh ok, I think I may have misunderstood your original question.

Yes you are correct that you will still start at index 0, but remember that there are essentially 2 sets of indexes, there are the positive ones that count from the beginning of the iterable, and the negative ones that count from the end. And when you are using a negative step from 0, you will be stepping through the negative ones.

Back to your numbers example, say we had the following slice:

>>> numbers[::-1]
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Since no start parameter is specified, we start at index 0. When you count backwards from zero with a step of -1, your first value will be the value at numbers[-1], then numbers[-2], and so on, until you reach numbers[-21], which is the end of the slice, but equivalent to the beginning of the iterable.

Thank you that makes a lot more sense now :)