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

create a new function named odds that returns every item with an odd index in a provided iterable.

please help on my error

def first_4(iterable):
    return iterable[0:4]

def first_and_last_4(iterable):
    first = iterable[0:4]
    last = iterable[-4:]
    result = first + last
    return result

def odds(iterable):
    reverse_list = iterable[2::]
    return new_list[::-1]

1 Answer

Steven Parker
Steven Parker
228,978 Points

Here's a few hints:

  • this function creates a "reverse_list" variable, but then tries to access "new_list" (not defined)
  • you can do this challenge using only one slice
  • since you want to return every other value, you'll need something other than the default "step" in the slice
  • you want all the odd indexed items, but that's not what a start value of "2" gives you
  • there won't be any negative slice parameters for this task