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

Albert Tomasura
Albert Tomasura
11,041 Points

slices.py Challenge Task 4/4

I tested the final piece ( def reverse_evens(items): ) in workspaces using the list in the example as: (items = [1, 2, 3, 4, 5]) and it produced [5, 3, 1], but it fails the challenge.

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

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

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

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

Hello! Albert Tomasura. You need to return the reverse list with all odd indexes. You just typed the code of bringing out random numbers. I'm sorry if you do not understand. But, you need to check these first:

Check if the list has even or odd total numbers using an if statement and using the len function

If they have even numbers then return the code.

If they have odd numbers then return different codes.

'''python if len(a) % 2 == 0: return Figure it out else: return Figure it out ''' If you need any help or you are still confused. Then let me know. I will give you the code!

Albert Tomasura
Albert Tomasura
11,041 Points

"You're on fire! Last one and it is, of course, the hardest. Make a function named reverse_evens that accepts a single iterable as an argument. Return every item in the iterable with an even index...in reverse. For example, with [1, 2, 3, 4, 5] as the input, the function would return [5, 3, 1]. You can do it!" - slices.py Challenge Task 4 of 4

Thanks for the help, Aryan. I wish they included those three checks to the Task description.

3 Answers

Here's the code I got to work. Basically I reversed the item first, then figured out whether its length was even or odd. I returned the appropriate value with the if statement.

def first_4(item):
    return item[:4]

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

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

def reverse_evens(item):
    reverse_item = item[-1::-1]
    if len(reverse_item) %2 == 0:
        return reverse_item[1::2]
    else:
        return reverse_item[::2]
Camilla Bendetti
seal-mask
.a{fill-rule:evenodd;}techdegree
Camilla Bendetti
Python Web Development Techdegree Student 892 Points

I am still confused about doing it in two parts? Why doesnt return items[::-2] work on its own? Aryan Monga can you explain what you meant by "if len(a) % 2 == 0: return Figure it out else: return Figure it out"

Sure!! " if len(a) % 2 == 0: return a[-2 ::-2] else: return a[-1 ::-2] " This code checks whether the variable a has even total numbers or odd total numbers. If it has even numbers then it will return a[-2 ::-2] otherwise it will return a[-1 :: -2]. It's that simple! ;)

Yeah!