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

Slices.py (4 of 4)

I'm not quite sure why this is not passing. When I run this in Python3 IDE I get the correct answer. Maybe a bug in the program?

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

def first_and_last_4(num):
    return num[:4] + num[-4:]

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

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

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

slicing by [::-2] doesn't return evens, it returns every other element from the end, which may or may not be the even indices. you can either test for length and cut off the last element of the list if the last index is odd, then slice every other element, or slice every other element from the start (which is always even - 0), then reverse. you can chain slice operators like[:5][::-3] etc.

Thanks James, chaining the operators was exactly what I was missing. Big help!