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

Vianney Gall
Vianney Gall
5,342 Points

I don't get it right on the challenge but it works on my workspace. How come?

For the part 4 of this challenge, my code does not work, but when I write this on my workspace, it works...

On my workspace it looks like this:

def reverse_evens(iterable): return iterable[::-2] print(reverse_evens(input("What is your iterable: ")))

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


def first_and_last_4(iterable):
    one = iterable[0:4]
    two = iterable[-4:]
    return one + two

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

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

2 Answers

Steven Parker
Steven Parker
231,269 Points

This method will work for some data, based on the length of the list. But to make it work for all data, you'll need one of these two strategies:

  • set the starting position based on the length (even or odd) of the list
  • take the even indexed items first, then reverse them as a separate operation

Either method will work when correctly implemented, but the second one is a bit easier to do.

Vianney Gall
Vianney Gall
5,342 Points

Excellent! Thank you so much!!!