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

Hi, i get the rigth result with my function reverse_evens : [5, 3, 1] but it's not validated. what is the mistake?

Hi,

i get the rigth result with my function reverse_evens : [5, 3, 1] but it's not validated. what is the mistake?

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


def first_and_last_4(item):
    list1 = item[0:4]
    list2 = item[len(item)-4:]
    list1.extend(list2)
    return list1

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

def reverse_evens(item):
    inverse = item[-1::-1]
    return inverse[0::2]

2 Answers

Hi,

For the last task, you need to get the evens first, then reverse that. Otherwise, you're relying on the passed-in iterable being of an odd numbered length.

Take [1, 2, 3, 4]. The method should return the even elements, reversed, i.e. [3, 1]. If you reverse it first, like you have done, you'll take the evens out of [4, 3, 2, 1] and get [4, 2].

So, get the evens, then reverse that.

Make sense?

Steve.

ok thanks :)