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

Bug in final step, or am I crazy?

For the final step of the slices.py challenge:

"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]."...

I'm getting the response that the test is returning the improper output. Meanwhile, I've tested this in workspaces on a list = [1, 2, 3, 4, 5], and gotten the correct output of a list containing [5, 3, 1]. So not sure what's going on here...

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

def first_and_last_4(random_iterable2):
    first_4 = random_iterable2[:4]
    last_4 = random_iterable2[-4:]
    return first_4 + last_4

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

def reverse_evens(random_iterable4):
    return random_iterable4[::-2]

2 Answers

Stuart Wright
Stuart Wright
41,119 Points

Your function works correctly if the input has an odd number of elements, but fails if the input has an even number of elements. If you pass it say [1, 2, 3, 4, 5, 6], it returns [6, 4, 2]. Those elements are at odd rather than even index in the list you passed in.

To complete this challenge takes two steps. First you need to extract those elements which are at an even index, then reverse only those elements.

Aha! So I am crazy. Thanks Stuart, that's extremely helpful.