Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,136 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