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 (Retired) Slices Slice Functions

Kirk Watts
Kirk Watts
2,202 Points

Returns items with an odd index?

I'm not too clear where I'm going wrong here - "Bummer!" doesn't really help me debug :). Thoughts? The Challenge says "Make a function named odds that accepts an iterable as an argument and returns the items with an odd index in the itterable."

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

def odds(kirk):
  for item in kirk:
    if (item.index() % 2) != 0:
      return item
    else:
      break

2 Answers

Hi Kirk, I see that you are having problems with Task 2 of the challenge. Here is what you should have done:

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

What this function does is start at the item at index 1 and goes up indexes in steps of 2. This will return all items at odd indexes. E.g. 1, 3, 5, 7, 9...

Kirk Watts
Kirk Watts
2,202 Points

Thanks Haider, makes sense that the challenge would want for me to use the stuff just taught to me.