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

Benjamin Guyton
Benjamin Guyton
6,858 Points

reverse evens

I feel like my approach is correct, as I've tested the results in the console. I also tried this without the conditional statement considering that the length of list_1 is even, so it doesn't seem necessary, but that did not prove to be correct either, though it also showed the desired list in the console.

slices.py
list_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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

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

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

def reverse_evens(list_1) :
    if len(list_1) % 2 = 0:
        return list_1[-2::-2]
    else:
        return list_1[1::-2]

1 Answer

I would suggest you to follow the last task word by word. ..

..

Here is a clue:

Identify your solution for this case and verify if your code gives the right answer.

case1: [1,2,3,4,5]:

case2: [1,2,3,4,5,6]

well, answer should be same for both.

.

.

.

Here is the answer...

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

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

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

#Following it word by word...
#every item with even index
#then reverse it
def reverse_evens(list_1) :
    return (list_1[0::2])[::-1]