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

Peter Alexander
Peter Alexander
4,283 Points

I'm struggling to complete the 4th Slice Function

I've tried various variations of the code without success, can someone point out the problem, thanks.

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

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

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

def reverse_evens(items):
    evens = items[0::2]
    return evens[-1::-1]

3 Answers

Peter Alexander
Peter Alexander
4,283 Points

Apologies, it's with reference to:

def reverse_evens(items): evens = items[0::2] return evens[-1::-1]

forgot my rules for a second. looks like this is correct. I got a pass for this challenge using your code

Mustafa Başaran
Mustafa Başaran
28,046 Points

Hi Peter,

The fist index in the slice is the starting point. So, you can omit the first -1 in the return statement in the last function.

 return evens[::-1]

The above statement will return the reversed version of the evens list. You can also take 0 out in

 evens = items[0::2]

The above slice is equal to

 evens = items[::2]

I hope this helps.