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

Jamaru Rodgers
Jamaru Rodgers
3,046 Points

My code works.

I know my code work. I just ran the same script in IDLE and it gave me the iterable items in their even indexes... in reverse. Not sure if I'm missing something specific here cause it won't take the code, but I know this works so I'm going to bed now :). Please help!

slices.py
def first_4(things):

    iterable_list = list(things)
    first4items = iterable_list[0:4]
    return first4items

def first_and_last_4(things2):

    iterable_list2 = list(things2)
    first4 = iterable_list2[0:4]
    last4 = iterable_list2[-4:]
    return first4 + last4

def odds(iterable):
    it_list = list(iterable)
    odd_index = it_list[1::2]
    return odd_index

def reverse_evens(numbers):

    number_list = list(numbers)
    eversing_list = number_list[::-2]
    return eversing_list

2 Answers

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

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

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

def reverse_evens(arg):
    return arg[::2][::-1]

You got the right idea.

Stuart Wright
Stuart Wright
41,120 Points

When your code appears to work in IDLE but does not pass the challenge, make sure you understand exactly what the challenge is asking you to do :)

Your function works correctly if the input has an odd number of elements, but fails if the input has an even number of elements. If you pass it say [1, 2, 3, 4, 5, 6], it returns [6, 4, 2]. Those elements are at odd rather than even index in the list you passed in.

To complete this challenge takes two steps. First you need to extract those elements which are at an even index, then reverse only those elements.

Edit: the solution provided by bronsonroybal shows how these two steps can be chained together on one line.