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

Fernandito Flores
seal-mask
.a{fill-rule:evenodd;}techdegree
Fernandito Flores
Python Web Development Techdegree Student 916 Points

Help understanding iterating in reverse order

I'm having difficulties completing a challenge task. The instructions are to make a function named reverse_evens that accepts a single iterable as an argument. Return every item in the iterable with an even index...in reverse. For example, with [1, 2, 3, 4, 5] as the input, the function would return [5, 3, 1].

The following accomplish this, at least that is what I believe based on my understanding of the task.

list1 = [1, 2, 3, 4, 5] print(list1[-1::-2])

prints out [5, 3, 1]

This is my code on the challenge task: def reverse_evens(reverse_even): return reverse_even[-1::-2]

Can anyone shed some insight on this?

slices.py
def first_4(four_items):
    return four_items[:4]

def first_and_last_4(first_n_last_4):
    first = first_n_last_4[:4]
    last = first_n_last_4[-4:]
    return first + last

def odds(oddly):
    return oddly[1::2]

def reverse_evens(reverse_even):
    return reverse_even[-1::-2]

1 Answer

andren
andren
28,558 Points

Your code accomplishes the task if the list in question has an odd number of items. But if it has an even number of items for example [1, 2, 3, 4, 5, 6] then the function would return [6, 4, 2], which are the numbers that have an odd index.

This is a challenge that confuses a lot of people, partly because it is worded in a somewhat confusing way, and partly because it requires you to do something not really shown off in the videos that precedes the challenge.

To solve this challenge you actually have to perform two slices. First slice the list with a step of two (not in reverse) and then reverse the resulting slice. Like this:

def reverse_evens(reverse_even):
    return reverse_even[::2][::-1]

That will result in always getting the numbers that have even indexes in the list, regardless of the number of items in the list.

Fernandito Flores
seal-mask
.a{fill-rule:evenodd;}techdegree
Fernandito Flores
Python Web Development Techdegree Student 916 Points

Thank you Andren. I didn't understand the task the way you explained it. It makes sense now. I wouldn't have come with a way of accomplishing this based on what we have covered so far. I see the logic behind it now. Thanks again.