Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.

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,021 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.