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

fahad lashari
fahad lashari
7,693 Points

Just need help with the last step

So I managed to solve the rest of the question however I am not sure as to exactly what is required to pass the last question.

the solution I came up with gives you exactly [5, 3, 1] if you have a list of numbers 1-5

Please do let me know

kind regards,

Fahad

slices.py
def first_4(string):

    return string[:4]

def first_and_last_4(string):

    return string[:4] + string[-4::1]

def odds(string):

    return string[1::2]

def reverse_evens(string):
    return string[-1::-2]

1 Answer

Steven Parker
Steven Parker
231,128 Points

There are a couple of different approaches to solving this.

Since you don't know how long the string will be, you can't assume which item will be the last with an even numbered index. Your code appears to work when the list has an odd number of items, but try it with a list with an even number.

So you have two choices:

  • you can compute the last even-indexed item using the length, and use that as the starting position
  • or you can use one slice to get only the even-indexed items, and another slice to reverse the list

Either way can pass the challenge, but the latter approach is likely to be the easiest.