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 (2016, retired 2019) Slices Slice Functions

Debidatta Gouda
Debidatta Gouda
3,019 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)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,989 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]
Welden Bringhurst
Welden Bringhurst
1,564 Points

Hi Debidatta thanks for asking this question and posting your code. I was having trouble with first_and_last_4 because i was reversing the order of my last 4 code and your solution helped me figure out I had interpreted the question improperly. I used some really silly and unintuitive names for my variables but here is my code and how I solved the last challenge. Cheers!

def first_4(cookie):
    return(cookie[0:4])
def first_and_last_4(biscuit):
    morsel = biscuit[0:4]
    smidgen = biscuit[:-5:-1]
    smidgen = smidgen[::-1]
    morsel.extend(smidgen)
    print(morsel)
    return(morsel)
def odds(wiggle):
    boggle = wiggle[1::2]
    return(boggle)
def reverse_evens(wiggle):
    boggle = wiggle[0::2]
    smoggle = boggle[::-1]
    return(smoggle)