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

I'm stuck unit3 - Challenge Task 3 of 4

Unit 3 Collections Challenge Task 3 of 4

OK, try this one on for size: create a new function named odds that returns every item with an odd index in a provided iterable. For example, it would return the items at index 1, 3, and so on. what could be the expected solution other than mine!

slices.py
def odds(iterable):

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

def last_4(iterable):
    return iterable[-4:]


def first_and_last_4(iterable):
    l1 = first_4(iterable)
    l2 = last_4(iterable)
    return l1 + l2


def odds(iterable):
    return iterable[0::2]


def is_odd(i):
    return (i % 2) != 0


def odds(iterable):
    results = list()
    index = 0
    for number in iterable:
        if(is_odd(index)):
            results.append(number)
        index += 1

    return results

4 Answers

Steven Parker
Steven Parker
229,732 Points

It looks like you have three different defintions for "odds", but you only need one. Your very last one will pass the challenge, but the purpose of the challenge is to practice using slices, and that solution does it with a loop instead.

Your second version was close, but it starts the slice on an even index (0) instead of an odd one.

Thank you Steven, I'm currently in the challenge 3 of 4 and it requested to use the last function in my code which is 'odds' however, the solution is refused, no idea why, i'm sure it's correct

any help :)

Steven Parker
Steven Parker
229,732 Points

The (third) solution shown here will work, but remove the other two "def odds".

Thank you Steven. Worked

I looked at multiple forums for this question and there is no solutions that work!

Steven Parker
Steven Parker
229,732 Points

Bear in mind that most postings to the forum will be code that does not work, and the poster is asking for help with it. One or more answers might help to fix the code, but they might not apply to your own code.

It's always good to scan the previous postings to see if someone had your same issue, but even if so, it will often be the hints given that will be of possible use to you. You may not see a complete solution, and in fact Treehouse discourages the posting of explicit solutions without explanation.