2
n=[1,2,3,4,5,6,7,8] 
n[:6:-2] #[8]

I think the result should be [6,4,2] not [8] and what surprised me more is when I slice the list explicitly n[0:6,-2] then the output is []

I've checked documents, and noticed shallow copy,thus still no idea what happend under the hood

Chweng Mega
  • 1,598
  • 1
  • 13
  • 22
  • What are you trying to achieve? Or this is just an understanding question? – DanielY Dec 10 '17 at 11:33
  • 2
    Possible duplicate of [Understanding Python's slice notation](https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation) – storaged Dec 10 '17 at 11:33
  • you might want to see this: [a link] https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation [a link] – Alessio Luciani Dec 10 '17 at 11:46

3 Answers3

1

To achieve [6,4,2] you need to go backwards on the list, so first you want six, its position on the list its 5, first part is done, second you basically want to iterate backwards 2 positions until there is no more list

n = [1,2,3,4,5,6,7,8]

n[5:0:-2] // result [6,4,2]

Remember that your trying to go backwards, so first should be the position that you start, second the end position and last the number of steps (-2).

Dunedan
  • 7,848
  • 6
  • 42
  • 52
Diogo
  • 11
  • 3
  • But this doesn't answer his question: why does `[n:6,-2]` result in `[8]`? – Jim Mischel Dec 10 '17 at 14:58
  • 1
    Well let's go through what your doing slowly, if you only did n[:6] you would get everything till the 6th position, [1,2,3,4,5,6], and if you only do n[::-2] you will get a reversed list with each second element, [8,6,4,2], when you put all together what happens is, you get the reversed list up until the 6th position which is only [8], if you add like a 9 and 10 also on the list you would get [10,8] I dont know if i explained myself well, please just let me know if you didn't understand i'll try to explain a bit better, i'm on a bit of a hurry atm – Diogo Dec 10 '17 at 17:38
1

Ok let me explain :

Before your problem let's understand two concepts of slice:

First concept :

step in slice :

a[start:end:step] # start through not past end, by step

Second concept :

and you can also do slice of slice :

n=[1,2,3,4,5,6,7,8]
print(n[3:][2:])

output:

[6, 7, 8]

Now back to your problem let's solve it step by step:

first reverse the string:

n=[1,2,3,4,5,6,7,8]
print(n[::-1])

output:

[8, 7, 6, 5, 4, 3, 2, 1]

then give the step = 2:

n=[1,2,3,4,5,6,7,8]
print(n[::-1][0:len(n):2])

output:

[8, 6, 4, 2]

now slice from the result whatever you want:

n=[1,2,3,4,5,6,7,8]
print(n[::-1][0:len(n):2][1:])

output:

[6, 4, 2]
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
  • thanks, so the sign of step represent the traverse direction(revert slice end and start) , and `abs(step)` is the traverse step length – Chweng Mega Dec 11 '17 at 03:52
  • @ChwengMega yes it represents same as i shown in that syntax. If my problem solved your issue you can accept the answer. – Aaditya Ura Dec 11 '17 at 04:30
0

If you want to iterate backwards, starting from the 3rd element from right, that's the correct notation.

n=[1,2,3,4,5,6,7,8] 
print n[len(n)-3::-2]
>[6, 4, 2]
user1767754
  • 23,311
  • 18
  • 141
  • 164