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

gibran erlangga
gibran erlangga
5,539 Points

make odd index in iterable question

Need a little help here guys.

I'm trying to solve this problem, but I'm stuck with this.

Any help will be highly appreciated. Thanks!

slices.py
def first_4(iterab):
    return iterab[:4]
def odds(iterab):
    a = iterab%2
    if a != 0:
        return odds

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi gibran erlangga! You're doing great and I can see you've given this some thought. But before I show you how I did it, let's take a moment and look at some things in your code first. Your odds function is trying to return itself. Also your variable a is not looking at the index of a place in your iterab variable. It's looking at the entire iterable. That being said, here's what you need:

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

This will start at the first index in our iterable. Remember the iterable has a 0 index, but we don't want that one. Then it will go down to the end of the iterable by steps of 2. So every other one :smiley: Hope this helps! :sparkles: