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 Python Collections (2016, retired 2019) Slices Slice Functions

aparently... event though [-1::-2] returned the same values as [::2][::-1] ....it was incorrect...

aparently... event though [-1::-2] returned the same values as [::2][::-1] ....it was incorrect...

why wasnt [-1::-2] acceptable, but [::2][::-1] is???

event though they return the exact same data???

1 Answer

Logan R
Logan R
22,989 Points

Hello!

So the meat of challenge 4 is: Return every item in the iterable with an even index...in reverse.

Given the list: [1, 2, 3, 4, 5], we want the following indexes: 0, 2, and 4. Our list would become [1, 3, 5]. Reversed, this is [5, 3, 1]. Your code works great for this example!

But what if our list is not an odd length. What if we had [1, 2, 3, 4, 5, 6] for example? We want the following indexes: 0, 2, and 4. Our list would become [1, 3, 5] then reversed to [5, 3, 1].

Here is where your code presents a problem.

>>> x = [1, 2, 3, 4, 5]
>>> y = [1, 2, 3, 4, 5, 6]

>>> x[-1::-2]
[5, 3, 1]
>>> y[-1::-2]
[6, 4, 2]

Your code starts at the last index, not at the last even index. In order to solve this, I would suggest separating it into 2 list commands (mylist[...][...]), one to get the even numbers and one to reverse the list.

I hope this helps! If you still don't understand anything, feel free to reply :)

(From my post yesterday: https://teamtreehouse.com/community/reverseeven)

Makes sense when you put it like that, thanks buddy