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

Pitrov Secondary
Pitrov Secondary
5,121 Points

I do not understand this 4th task.

I think that I misunderstood the Task, cause I think my code is right, but I do not know why it does not accept it. Can you help me with this last step?

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

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

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

def reverse_evens(list):
    return list[::-2]

2 Answers

Hai Phan
Hai Phan
2,442 Points

Because we never know if the last item in the list given was even or odd index, but you always know the first item in the list is even index(0), so what you need to do is find all even index items just like normal and then reverse them. Also you can done it in one line

def reverse_envens(something):
    return something[0::2][-1::-1]
Pitrov Secondary
Pitrov Secondary
5,121 Points

But if you use 2 brackets like this [] [] then it looks for something in that item? I am confused, if you understood what I meant, can you explain why you can do it in one line?

Hai Phan
Hai Phan
2,442 Points

Actually, it just like you use list[::2] first to create an evens_list and then use do evens_list[-1::-1] to reverse them. This code just combines them, and it's not looking for something in that because it's not [][], it is [start:stop:step][start:stop:step] which is return you a slice of a slice.

P/s: I'm also new in this stuff but I've tried and found this thing work!

Hi, I just had the same issue, apparently you need the even the list first with a

list[::2]

and then reverse it like

list[-1::-1]