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
207,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