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

Abel Sila
Abel Sila
1,584 Points

task 4 of 4 on slice

I do not know the answer to this and I think it might have an error. I have tried the code in my laptop and the code does what it suppose to do for this question, but it keeps on getting giving me "I got it wrong" when I click Check work, Help me find the answer to this task, please.

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

def first_and_last_4(lists):
    first_4 = lists[:4]
    last_4 = lists[-4:]
    first_4.extend(last_4)
    return first_4

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

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

2 Answers

Steven Parker
Steven Parker
231,275 Points

The trick to this challenge is that the length of the list (or more specifically if it has an odd or even number of elements) will affect how to accomplish the goal. There are two ways to handle this:

  • you can compute the starting position based on the length
  • you can slice the even indexed items first, and then reverse them in a separate operation

Either technique will pass the challenge when implemented properly, but the 2nd one is a bit easier to code.

Abel Sila
Abel Sila
1,584 Points

thank you Steven