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

Péter Juhász
Péter Juhász
4,768 Points

reverse_evens gave an error message

Why the reverse_evens is not correct. I tried in IDLE with the example and it worked for me. Note: The example gives backs the odd items in reverse order. I tried with return iter4[-2::-2] which give back the evens in reverse order but it didn't work.

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

def first_and_last_4(iter2):
    return iter2[:4] + iter2[-4:]

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

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

3 Answers

Steven Parker
Steven Parker
231,248 Points

The code shown will work when the argument has an odd number of elements. Otherwise, it will return "reverse odds" instead.

To always return reverse odds, there's two basic strategies that are equally effective: you can compute the starting position based on the size, or you can extract the even indexes first and then reverse them in a separate step.

Péter Juhász
Péter Juhász
4,768 Points

Thanks. I returned the following slice in my code and it was accepted. in_string[::2][::-1] It returns odd elements in the reverse order so the name of the function is still wrong!!

Steven Parker
Steven Parker
231,248 Points

That code should always return the items with even-numbered indexes (reversed), which is what the challenge asked for. Good job! :+1:

Péter Juhász
Péter Juhász
4,768 Points

Thanks. I understood now.