Python Python Collections Slices Slice Functions

Debidatta Gouda
Debidatta Gouda
2,307 Points

in my editor i am able to get the correct answer but why here it showing error

can any one help me this

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

def first_and_last_4(number):
    last=len(number)-4
    result=number[:4]+number[last:]
    return result
def odds(number):
    return number[1::2]
def reverse_evens(number):
    return number[::-2]

new_list=[1,"debi",56,True,56,"akib","raju",566,0,325,65]
result=first_4(new_list)
result=first_and_last_4(new_list)
result=odds(new_list)
resul=reverse_evens(new_list)
print(resul)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 51,828 Points

The reverse_evens can be tricky. Because you don't know in advance the length of the list passed in, you can not simply reverse the list as the result may be the odd indexes or the even indexes depending on length. First get the even indexes then reverse those. To test your code on try running:

reverse_evens([0, 1, 2, 3, 4, 5])
# should return [4, 2, 0]
reverse_evens([0, 1, 2, 3, 4, 5, 6])
# should return [6, 4, 2, 0]