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

Soumik Rakshit
Soumik Rakshit
2,745 Points

The last function is not passing on Treehouse but is giving correct answer on the Python 3.6 IDLE. Please Help.

The last function passes the list [1,2,3,4,5] and asks the output [5,3,1]. I returned a[::-2] but it is not passing on Treehouse but is giving correct answer on the Python 3.6 IDLE.

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

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

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

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

1 Answer

Steven Parker
Steven Parker
231,236 Points

:warning: Be careful about testing a challenge in an external IDLE or REPL.

If you have misunderstood the challenge, it's also very likely that you will misinterpret the results.

In this case, you did not consider that the correct offset would be different for a list with an even number of items compared to one with an odd number of items. To produce the correct result in both cases, you'll need to either compute the offset based on the length of the list, or break the task up into steps and use two slices.