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

why odds function is not working properly

i used odds functions to change data with odd indexes datas and then return it but still it's not working

slices.py
def first_4(data_list):
    data_arr=data_list[0:4]
    return data_arr[0:4]
def first_and_last_4(data_list):
    data_first_four=data_list[0:4]
    data_last_four=data_list[-5:-1:1]
    data_final=data_first_four+data_last_four
    return data_final
def odds(data):
    data_odd=data[1::2]
    return data_odd

1 Answer

Not sure why it isn't working but I did this and it worked for me:

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

No need to assign the new list to a new variable before returning it, we can just return it in a single line.