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

daostryver
daostryver
1,967 Points

Reverse evens of a argument

how do i get reverse evens of a argument when i return the argument

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

def first_and_last_4(string):
    left = string[:4]
    right = string[-4:]
    return left + right

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

def reverse_evens[evens]:
    return evens[::-2]

3 Answers

Hello daostryver, this one caught me out also. If you first find every second character and then reverse the result this will give you the correct value to return. This is because (and this is what i failed to see) you will get a different result if your string has an even number of characters. something like:

string = "a,b,c,d,e"
evens = string[::2]
r_evens = evens[::-1]

# you can do this in one line also like this
string = "1,2,3,4,5"
r_evens = string[::2][::-1]

Hope this helps. It had me stumped for a while.

daostryver
daostryver
1,967 Points

def reverse_evens[evens]: list1 = evens[::2] list1.reverse() return list1

i've also tried this. im stumped

Ernestas Petruoka
Ernestas Petruoka
1,856 Points

If you still stuck then check out my explaination to other student because I do not want repeat myself and if you still have some questions fell free to ask :)

https://teamtreehouse.com/community/i-cant-get-past-this-question-im-getting-a-generic-try-again-message