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

Arun Patel
Arun Patel
1,180 Points

What's wrong with reverse_evens function?

I used [::-2] to slice and get the characters reversed with even indexes. I tried separately with the string "arunpatel". I got the value as ['l', 't', 'p', 'u', 'a']. I assumed same should have worked here.

slices.py
def first_4(str1):
    str2 = list(str1)

    if len(str2) >= 4:
        list1 = str2[:4]
    else:
        return
    return list1


def first_and_last_4(str1):
    str2 = list(str1)
    if len(str2) >= 8:
        list1 = str2[:4] + str2[-4:]
    else:
        return
    return list1


def odds(str1):
    str2 = list(str1)
    list1 = str2[1::2]
    return list1

def reverse_evens(str1):
    str2 = list(str1)
    list1 = str2[::-2]
    return list1

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The issue is being able to handle inputs that have both an even and odd number of items. If you try "arunpate", you'd get ['e', 'a', 'n', 'r']. Think about breaking the problem into two parts. 1) extract the even index items, then 2) reverse this sublist.

Post back if you need more help. Good luck!!!