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

Norbert Asomani
PLUS
Norbert Asomani
Courses Plus Student 878 Points

Completing Task 4 on Code Chalenge for Python Slice Functions

I am studying Python Collections currently on slice functions. There is one task (Actually task 4) where I am supposed to create a function to display all even arguments of an iterable item in reverse order. The example given is to display [5, 3, 1] from [1, 2, 3, 4, 5].

I have created a function as follows:

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

Somehow I get the response that the values returned are wrong. When I run this on my compiler however I get same as expected in question.

3 Answers

If you search the forum you'll find this question has been already answered.

But let me give you a certain scenario.

list:     1,2,3,4,5          1,2,3,4,5,6
[::-2]   5,3,1                6,4,2
[0::2]  1,3,5                1,3,5

#you can see that picking elements from end of list isn't same as picking from start and then reversing the list.

Solution...? Follow the instruction line by line.

Since you haven't specified the challenge so i am not sure for what exactly was the question but this is what I remember, was one of the task in python collection which involved alternate numbers and then reversing the list.

Share the link to the challenge if problem persists.

Brian Peterson
seal-mask
.a{fill-rule:evenodd;}techdegree
Brian Peterson
Python Web Development Techdegree Student 13,634 Points

@Krishna. Very good catch. It is a subtle logical difference that it took me a while to understand.

So, list[::-2] works to solve the problem (i.e. create a reversed list of items located on all the even index locations in the list) only if the list contains an odd number of items. The code challenge baits you into choosing this seemingly correct solution strategy by providing a test case where that solutions works (e.g. list = [1, 2, 3, 4, 5]). But in reality that solution will fail 50% of the time. Specifically, it will fail if the list has an even number of items. If the list has an even number of items then list[::-2] will produce a list of reversed items located at the odd indexes (the exact opposite of what is called for in this problem).

Therefore, the correct solution strategy is to do what Krishna suggested above. You count the even indexes normally first (list[::2]) and then reverse the list (.reverse()) in separate step. This strategy produces the correct result regardless of whether the list has an odd or even number of items.

Norbert Asomani
PLUS
Norbert Asomani
Courses Plus Student 878 Points

Thank you so much Krishna. I finally managed to complete it. Like you said, key was first taking the even arguments and then reversing the list.

Glad, i could be of some assistance. Enjoy!