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

how can i make a function that reverse evens in index?

Am stuck and need some help on slices task 4

slices.py
def first_4(Paida):
    return Paida[0:4]
def first_and_last_4(Munyaradzi):
    return Munyaradzi[0:4] + Munyaradzi[-4:]
def odds(king):
 return king[1::2]

1 Answer

It is like this:

value[::-1]

In a function:

def reverse(value):
    return value[::-1]

So if you type inn:

reverse([1, 2, 3])

You get:

[3, 2, 1]

So the whole task (1-4) is:

def reverse_evens(value):
    value_rev = value[::2]
    return value_rev[::-1]

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

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

def first_4(value):
    return value[:4]