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

reverse_evens

You're on fire! Last one and it is, of course, the hardest. Make a function named reverse_evens that accepts a single iterable as an argument. Return every item in the iterable with an even index...in reverse. For example, with [1, 2, 3, 4, 5] as the input, the function would return [5, 3, 1]. You can do it!

Can someone help with reverse Challenge Task 4 of 4. Its not passing

def first_4(list_items):
    return (list_items[0:4])

def first_and_last_4(item):
    return item[:4] + item[-4::1]

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

def reverse_evens(item):
    item.reverse()
    return (item[::2])
>>> item
[1, 2, 3, 4, 5]
>>> i= list(item[0::2])
>>> i
[1, 3, 5]
>>> item = [1, 2, 3, 4, 5]
>>> item.reverse()
>>> item[::2]
[5, 3, 1]

It work on my computer

3 Answers

Slice Notation

print(agr4[::-2])
Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

What happens when you have an even number of items?

>>> item = [1, 2, 3, 4, 5, 6]
>>> item.reverse()
>>> item
[6, 5, 4, 3, 2, 1]
>>> item[::2]
[6, 4, 2]

It gives the "even items of the reverse" but it should be the "reverse of the even items".

Hint: Try getting the even indexed items first, then reverse the result.

def reverse_evens(agr4):
    i = list(agr4[::2])
    return (i.reverse())

Still it is not passing.

>>> agr4 = [1, 2, 3, 4, 5,6]
>>> i = list(agr4[::2]).reverse()
>>> i
>>> agr4
[1, 2, 3, 4, 5, 6]
>>> i = list(agr4[::2])
>>> i
[1, 3, 5]
>>> i.reverse()
>>> i
[5, 3, 1]

https://teamtreehouse.com/library/python-collections-2/slices/slice-functions

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Instead of reverse(), try using slice notation to reverse the list: [::-1]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

reverse() method modifies a list in place. Its return value is None. So, the statement:

return (i.reverse())

returns None

Thank you Chris Freeman it worked out