44

Possible Duplicate:
What is :: (double colon) in Python?

I read the question What is :: (double colon) in Python when subscripting sequences?, but this not answer what myarray[x::y] mean.

nbro
  • 13,796
  • 25
  • 99
  • 185
Framester
  • 29,970
  • 48
  • 127
  • 186
  • 2
    It appears to me that other question does answer yours as well. `myarray[0::3]` is extended slice syntax that means start at element 0, step by 3, and stop at the end of `myarray`. – Dave Costa Aug 19 '11 at 15:31
  • However, it really looks like the answer you linked is also valid for your question: get every `y`th element of a list, starting at the `x`th element – mdeous Aug 19 '11 at 15:32

1 Answers1

83

It prints every yth element from the list / array

>>> a = [1,2,3,4,5,6,7,8,9]
>>> a[::3]
[1, 4, 7]

The additional syntax of a[x::y] means get every yth element starting at position x

ie.

>>> a[2::3]
[3, 6, 9]
GWW
  • 41,431
  • 11
  • 106
  • 104