34

Possible Duplicate:
Good Primer for Python Slice Notation

I've been reading over some sample code lately and I've read quite a few websites but I just can't seem to get the query right to give me an answer I'm looking for. If anyone could help me out I'd appreciate it.

Community
  • 1
  • 1
pri0ritize
  • 554
  • 2
  • 7
  • 19
  • If you do a little bit of searching you'll find lots of post with similar questions (and similar answers). I think this one must be closed – juliomalegria Jan 27 '12 at 02:02

6 Answers6

42

It slices

x[startAt:endBefore:skip]

if you use skip = 2, every other element the list beginning at startAt and ending at endBefore will be selected. [Remember: indices live BETWEEN list elements]

To see this, enter

x = range(100)

at the Python prompt. Then try these things

x[::2]
x[::3]
x[10:40:6]

and see what happens.

ncmathsadist
  • 4,684
  • 3
  • 29
  • 45
41

L[x::y] means a slice of L where the x is the index to start from and y is the step size. Here are some examples you can try in the interpreter

>>> L=range(20)
>>> L
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

If you want every 3rd element

>>> L[::3]
[0, 3, 6, 9, 12, 15, 18]

Now every third element starting from L[1]

>>> L[1::3]
[1, 4, 7, 10, 13, 16, 19]

Now every third element starting from L[2]

>>> L[2::3]
[2, 5, 8, 11, 14, 17]

You can specify a negative step to go backwards

>>> L[::-1]
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

You can also assign to this slice, but the value must have the same length as the slice you are replacing

>>> L[::3]=[0,0,0,0,0,0,0]
>>> L
[0, 1, 2, 0, 4, 5, 0, 7, 8, 0, 10, 11, 0, 13, 14, 0, 16, 17, 0, 19]

Finally, you can delete every 3rd element like this

>>> del L[::3]
>>> L
[1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19]
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
4

This is the syntax for list slicing. When you say,

list[a:b:c],

a is the starting index, b is the ending index and c is the optional step size. This will give you a list starting at index a(inclusive) and ending at index b(exclusive) picking elements at a step of c.

For example,

l = [1,2,3,4,5,6,7]

If I say l[2:6:2], this will give me [3,5].

If you skip the end index, like in your question, it would take elements from the start index (x), pick every yth element until it reaches the end of the list if y is positive and beginning of the list if y is negative.

E.g. l[1::-1] = [2,1]

l[1::2] = [2,4,6]

The default step size is 1.

Divya
  • 2,594
  • 1
  • 17
  • 29
1

It means create a new list from x until the end with step y:

>>> l = range(10)
>>> list(l[2::2])
[2, 4, 6, 8]
Rob Wouters
  • 15,797
  • 3
  • 42
  • 36
0

Returns a list containing every yth element of list starting at index x.

>>> alist = range(10)
>>> alist[0::2]
[0, 2, 4, 6, 8]
philofinfinitejest
  • 3,987
  • 1
  • 24
  • 22
0

This is a Slice.

[start:end:step]

Leaving any blank puts them at a default value, in your case it is taking every y elements starting at x and going until the end of the list.

See: What is :: (double colon) in Python when subscripting sequences?

Community
  • 1
  • 1
Jeff B
  • 29,943
  • 7
  • 61
  • 90