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

cant get the elements with odd index from a list

def first_4(x): return x[:4]

def first_and_last_4(x): return x[:4] + x[-4:]

def odds(x): list_1 = [] for item in x: if x.index(item) == 1 or x.index(item) % 3 == 0: list_1.append(item) return list_1

do not understand what is wrong with a code. it just says: "did not ge thte right values as an error"

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

def first_and_last_4(x):
    return x[:4] + x[-4:]

def odds(x):
    list_1 = []
    for item in x:
        if x.index(item) == 1 or x.index(item) % 3 == 0:
            list_1.append(item)
            return list_1

1 Answer

Steven Parker
Steven Parker
231,275 Points

Yea, those "Bummer" messages don't always give a useful clue. But the best hint I can give is that this can be done with a single slice.

Just think about how you might use the slice parameters of start, stop and step. If you set them right, the slice will give you only every other value starting with the first odd numbered index.

Bonus hint: when you get to #4, one way to solve it involves two slices. :wink: