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

the odds() problem does return the value of the odd index, on my local machine but on treehouse challenge, not so.

def odds(z): for i in z: index = z.index(i) if index % 2 != 0: return i note: i get the error msg. "didnt get the right values"

slices.py
def first_4(x):
    return x[:4]
def first_and_last_4(y):
    first = y[:4]
    last = y[-4:]
    return first + last
def odds(z):
    for i in z:
        index = z.index(i)
        if index % 2 != 0:
            return i

# I realized that the assumptions are the numbers are in descending or ascending order, that are not "messy".
#There fore, works and passed challenge,
#I was wrong in my assumptions, not the calculations. The above works if you dont know where the odd indexes are, if #you were to have list that extend or append lists, the above may be useful.  I made it difficult.  

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

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

using the slice notation is much simpler. the [] can take a third argument that is the 'step', how far to go to the next element. [ begin : end : step ]