60

I met this in a python script list[:, 1] and I am trying to figure out the role of the comma.

thefourtheye
  • 221,210
  • 51
  • 432
  • 478
andgeo
  • 1,065
  • 4
  • 10
  • 16
  • 18
    That's a numpy syntax. http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html – Ashwini Chaudhary Jan 16 '14 at 15:21
  • specifically, that command is accessing two different dimensions of the data structure (rows and columns) – Paul H Jan 16 '14 at 15:23
  • 1
    This syntax will raise `TypeError: list indices must be integers, not tuple` , so I'm sure the object was not a regular Python list. – Paulo Scardine Jan 16 '14 at 15:24
  • 1
    hmm, you ask about lists, but it has the numpy tag on it, and your syntax works only on numpy arrays instead of lists. I assume you do not understand the difference between numpy arrays and the python lists, and thus your question? – usethedeathstar Jan 16 '14 at 15:29
  • 2
    @usethedeathstar Ashwini added the numpy tag on the assumption that this is a numpy array. – poke Jan 16 '14 at 15:32
  • @poke Yes, but than the syntax in the question should be changed as well, since it is not list[:,1] but arr[:,1] since now it is just going to confuse people – usethedeathstar Jan 16 '14 at 15:52
  • @usethedeathstar In the end, it’s OP asking the question though… – poke Jan 16 '14 at 16:17
  • possible duplicate of [Python's slice notation](http://stackoverflow.com/questions/509211/pythons-slice-notation) – mgilson Jan 16 '14 at 16:36
  • You are right, I now realise this is indeed a numpy array, not a list, and thus must be a numpy syntax. Got it, thanks for spending time with me! – andgeo Jan 16 '14 at 16:43
  • It's still Python syntax, not NumPy syntax. NumPy just supports it. – mkrieger1 Dec 03 '21 at 13:07

3 Answers3

53

Generally speaking:

foo[somestuff]

calls either __getitem__, or __setitem__. (there's also __getslice__ and __setslice__, but those are now deprecated, so let's not talk about that). Now, if somestuff has a comma in it, python will pass a tuple to the underlying function:

foo[1,2]  # passes a tuple

If there is a :, python will pass a slice:

foo[:]  # passes `slice(None, None, None)`
foo[1:2]  # passes `slice(1, 2, None)`
foo[1:2:3]  # passes `slice(1, 2, 3)
foo[1::3]  # passes `slice(1, None, 3)

Hopefully you get the idea. Now if there is a comma and a colon, python will pass a tuple which contains a slice. in your example:

foo[:, 1]  # passes the tuple `(slice(None, None, None), 1)`

What the object (foo) does with the input is entirely up to the object.

mgilson
  • 283,004
  • 58
  • 591
  • 667
  • 1
    what a boss explanation :D I've just read PEP8 and I've reached the pet peeves part where it says that in slices, the colon : acts as a binary operator, so I immediately googled more and landed here, now I see how the [] subscription method is creating a slice with colon between numbers when there are multiple objects.. – Marius Mucenicu Apr 17 '18 at 05:18
  • 1
    Would be helpful to add that for a numpy array (which is where most people will encounter this), `foo[:, 1]` will return the column of the 2d array at index 1 (and will throw an exception if foo is not a 2d array). – Daniel C Jacobs Dec 10 '21 at 17:51
43

Lets assume list is a 2D (numpy) array as follows:

[[ 1, 2, 3],
 [ 4, 5, 6],
 [ 7, 8, 9]]
list[1,1]  # --> 5

It says select the element in position [1,1] (note that indexes start from zero)

list[:,1]  # --> [2,5,8] 
list[1][1]  # --> 5
list[:][1]  # --> [4 5 6]

See this and this for further examples.

aklingam
  • 135
  • 9
qartal
  • 1,904
  • 16
  • 30
4

In a sense the comma separates the different dimensions of your array that you are trying to select from.

Lets say I have a 2D array

my_array = numpy.array([[1,2,3],
                        [4,5,6],
                        [7,8,9]])

I could select rows(0 and 1) and columns(1 and 2) by doing this:

#             rows | cols
print(my_array[0:2, 1:3]) # prints [[2 3]
                                    [5 6]]