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

for create function reverse_evens, how come this is not acceptable: def reverse_evens(iter): return iter[::-2]

for create function reverse_evens, how come this is not acceptable: def reverse_evens(iter): return iter[::-2]

slices.py
def first_4(iterable):
    return iterable[:4]
def first_and_last_4(iterable):
    first_4 = iterable[:4]
    last_number = iterable[-1:]
    last_3 = iterable[-4:-1]
    last_4 = last_3 + last_number

    added = first_4 + last_4
    return added
def odds(iterable):
    return iterable[1::2]
def reverse_evens(iterable):
    return iterable[::-2]

3 Answers

Steven Parker
Steven Parker
231,007 Points

You don't know if you're getting evens or odds.

That would certainly give you every other item in reverse, but whether those items are from the even indexes or the odd indexes would depend on how long the list is.

You'll need to either compute where to start based on the length, or you'll need to get the evens first and then reverse them in a separate operation (perhaps by using two slices).

Hi Steven

I kinda see where you are getting at, however, I just keep getting it wrong. Do you mind sharing your code with over here (the even odd slicing question). Would be great!

Thanks!

oh ok thanks!

Hey Dean,

this is how I ended up implementing the function:

def reverse_evens(itera):
    count = 0
    for i in itera:
        count = count + 1
    if count % 2 == 0:
        print(itera[-2::-2])
        return itera[-2::-2]
    else:
        print(itera[::-2])
        return itera[::-2]
Steven Parker
Steven Parker
231,007 Points

You don't need to print anything for this challenge. But otherwise, good job. :+1: