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

The code seems correct but the challenge task prompts an error---"Bummer. Could'nt get the right values"

I am getting an error that the function reverse_evens() could'nt return the right value. Please help...

slices.py
def first_4(items):
    return items[0:4]
def first_and_last_4(items):
    return items[0:4] + items[-4:]
def odds(items):
    return items[1::2]
def reverse_evens(items):
    return items[::-2]

1 Answer

This is a common mistake made by beginners.

items[::-2] doesn't always return the "reverse evens!"

Watch:

>>> lst1 = [1, 2, 3, 4]
>>> lst2 = [1, 2, 3, 4, 5]
>>> lst1[::-2]
[4, 2]
>>> lst2[::-2]
[5, 3, 1]

Notice when you enter lst1[::-2], it indeed returns the "reverse evens." But, notice that lst2[::-2] returns the reverse odds!

The correct way to get the reverse evens is with a simple items[::2][::-1].

items[::2][::-1]

The [::2] bit gets the even-indexed items, and the [::-1] bit reverses the evens.

I hope this helps.

It worked!!!

Thank you. That was very helpful.

Your welcome :grin: