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

Han Li
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Han Li
Python Web Development Techdegree Graduate 14,817 Points

First Slice

Hi,

I got the following error

"Bummer: Didn't get the right values from reverse_evens"

Thanks, Ross

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

def first_and_last_4(items):
    return items[0:4] + items[-4:]

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

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

1 Answer

Steven Parker
Steven Parker
229,732 Points

This strategy will work with some test data, but not every time.

To create a function that will work reliably with all data, there are two basic approaches:

  • compute which item to start with based on the size (or actually the "even or odd-ness") of the list, or
  • use one slice to extract all the even indexed values, and then another slice to reverse them

Either method will work when implemented correctly. Bonus hint: the second method might be easier to implement.