List Slicing


So. In the 17.17 lesson told us to slice. Ok. Is't [::-1] a backward slicing? What do I miss?

Here is my code:

garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
message = "".join(list(filter(lambda x: x != "X", garbled))[::-1])

print message

The output of code:

I am the secret message!

And server says me:

Oops, try again. Make sure to use list slicing and a negative stride in your answer!

But I'm already use the negative stride! Or am I wrong?


Hi, @netscum ,

Yes, it is a negative stride. It might not be the same negative stride that Codecademy is expecting.


here is the exercise url:

if i run the code, i get:

I am the secret message!

@netscum, seems fine, maybe you hit a glitch? try refreshing the page or a different browser

you can also try to leave out lamba? if you use a negative stride with steps of 2, X gets skipped


Hi @appylpye and @stetim94.
Many thanks for your responses.
Found another way that been accepted.

message = garbled[::2][::-1]


@netscum ,

Your solution is a very nice one, however it appears that Codecademy wants a slice to be applied directly to garbled rather than to an object derived from garbled.

Edit: Yes, we can see in the above that Codecademy accepts a solution that applies a slice directly to garbled. @netscum, that's an interesting application of a slice to a slice, but it can be refactored to make it simpler.


We can't go [::-2] because it's give us only XXX...
Or not...

UPD: Oh... Sorry. Checked it and it work fine to. Thanks xD
I'm dummy...


yea, it should work. Your first solution was really nice,but as appylpye tells you, the exercise won't approve this, but your solution is correct :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.