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

Stuck on last task of the slices challenge

I am stuck on the last task of the slices challenge and I can’t seem to figure out, what I am doing wrong. For the reverse_evens() function my solution never passes. I tested it in workspaces and it seemed to give me exactly the reversed order of even indexes as requested. Can someone help me out, please?

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

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

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

def reverse_evens(iterable):
    return iterable[::-2]

2 Answers

Ines Fazlić
seal-mask
.a{fill-rule:evenodd;}techdegree
Ines Fazlić
Python Web Development Techdegree Student 9,569 Points

Hi Moritz, you have to check first if the iterable has even number of items or indexes. because if the last index is odd index number it will return the odd index numbers and not the even index numbers. you can check it with %2 which returns the remainer of number devised by two. if there is a remainder then there are odd number of items in the given iterable and you adjust your code for each possibility. Hope it helps :)

Thank you so much! I was not thinking about different iterable lengths at all. That helped me complete the challenge! :)

Sondre Fjellving
Sondre Fjellving
4,700 Points

Thanks a lot Ines, this helped me too!