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

Sanjay Noronha
Sanjay Noronha
7,296 Points

Python Slices reverse_evens problem : Not able to proceed

Hi Guys, Am stuck on this problem for a few days now. I believe my answer is correct but the system does not think so. Could you please let me know if my code is incorrect else let me move ahead. I am feeling pretty stuck here.

Cheers.

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

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

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

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

2 Answers

Thomas Fildes
Thomas Fildes
22,687 Points

Hi Sanjay,

The way I solved this challenge was to use the following code below:

inputValue[::2][::-1]

The code above allows you to take the even numbers first and then reverse it afterwards

Hope this helps! Happy Coding!!!

Sanjay Noronha
Sanjay Noronha
7,296 Points

Thank you Thomas. This was very helpful. I did not grasp the requirement but I now do.

your code return even index only when last index of the iterable is even. But if the last index is odd? Try an if statement or just play whit slice/slice

Sanjay Noronha
Sanjay Noronha
7,296 Points

Thank you very much. I certainly did not grasp the requirement in total. Go well.