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

Help with challenge #4! How can I use the negative index function if I don't know how long the list will be?

Hi! I'm stuck on the fourth step of this challenge. To use the negative step function, do I need to use negative starts to start at the end of the list? If that's the case, how do I know the list length so I can get only the even indicies? Do I need to use an if/then and % by length?

slices.py
def first_4(iterable):
    iterable = list(iterable)
    return iterable[0:4]

def first_and_last_4(iterable):
    iterable = list(iterable)
    first4 = iterable[0:4]
    length = len(iterable) - 4
    last4 = iterable[length: ]
    result = first4 + last4
    return result

def odds(iterable):
    iterable = list(iterable)
    return iterable[1::2]

def reverse_evens(iterable):
    iterable = list(iterable)
    listinreverse = iterable[::-1]
    listbytwos = listinreverse[2::2]
    return listbytwos

1 Answer

Brennan White
Brennan White
11,646 Points

You are on the right track. You can work from what you did for odds(). For reverse_evens(), find the evens first, then reverse them. That way it doesn't matter how many are in the iterable. *Also, note that the set begins at 0, not 1. So if you do evens [2::2], you will miss the first term.