Reverse list slice (like[x:y:-z]) in Python | Sololearn: Learn to code for FREE!

+1

Reverse list slice (like[x:y:-z]) in Python

Hello from JPN. I'm very beginner of Python. Please help me. I have written the code below to see how REVERSE list slice behaves, but couldn't understand why the result is like this. ### my code ### letters = "abcde" try1 = letters[:3:-1] try2 = letters[2::-1] print(try1) # this prints "e" ...why??? print(try2) # this prints "cba" ...WHY???!! ### end of my code ### Could anyone please explain?

5/16/2017 9:36:51 PM

sanochihi

4 Answers

New Answer

+6

try1 means 'slice it from the last element to the 3rd, but not include the third'. Since the last one is 'e' and it's an element of an index 4, it gets printed and the command seeks another one to the left (you specified the step to negative 1). It goes to 'd' but it has an index of 3 and you set the boundary to 3. It does not get printed and the command ends. try2 prints out starting from the index 2 element ('c') and goes leftwards until it reaches the end of the slice (element index 0).

+5

The syntax for slices is [start:stop:step] Start is the index value of the first item of the slice. Remember that 0 is the first index value (actually an offset, being 0 items away from the first item). If the start place is blank, it defaults to the first item, i.e. the item closest to the "[" or the "]", depending on which direction you are going. Stop is the index value of the end of the slice. This is NOT included in the slice. If the stop place is blank, it defaults to the end of the list, and the last item IS included in the slice. I'm not going to go into negative start and stop values because your code doesn't use them and things would get unnecessarily complex, but for sure, try them out in the code playground. Step defaults at 1. If it is 2, the slice includes every second item. If it's negative, the slice goes from right to left. A couple of examples: letters[:::] gives abcde letters[::-1] gives edcba index|0|1|2|3|4 letter|a|b|c|d|e When the step is -1, the indexing doesn't change, i.e. the leftmost item is still 0. So in your code letters[:3:-1], step -1 means you are going from right to left start is blank, meaning the first item is the item closest to "]", i.e. "e" stop is 3. Letter at index 3 is "d" So starting at the right at "e" and going to the left up to but not including "d" just gives you "e". And in your code letters[2::-1] step -1 means you are going from right to left start 2 means you start at index 2, which is "c" stop is blank so you go all the way to and including the end, which is "a" So the output is "cba" Hope this helps.

+3

@sanochihi You're welcome!

+2

@Kuba Siekierzyński @David Ashton Thank you you very much. I've understood. >>step -1 means you are going from right to left That’s what I didn’t and wanted to know. I was thinking that [::-1] is only a matter of output order. Before this question, I was thinking [:3:-1] make “abc” at first, and then, turn it like “cba”. That’s the point I was misunderstanding. Actually, [::x] is a matter of searching direction. If plus, it goes from left to right, and if minus, right to left. Now it’s very clear. Thank you very much!!