List Slicing


#1

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!
None

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?


#2

Hi, @netscum ,

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


#3

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


#4

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

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

#5

@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.


#6

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...


#7

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:


#8

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