5

Sample code:

import numpy as np
a = np.zeros((5,5))
a[[0,1]] = 1     #(list of indices)
print('results with list based indexing\n', a)

a = np.zeros((5,5))
a[(0,1)] = 1   #(tuple of indices)
print('results with tuple based indexing\n',a)

Result:

results with list based indexing
 [[ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]]

results with tuple based indexing
[[  0.   1.   0.   0.   0.]
 [  0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.]]

As you must have noticed, indexing array with list gave a different result than with tuple of same indices. I'm using python3 with numpy version 1.13.3

What is the fundamental difference in indexing a numpy array with list and tuple?

2 Answers 2

7

By design. Numpy's getitem and setitem syntax does not duck-type, because the different types are used to support different features. This is just a plain old __setitem__:

a[(0,1)] = 1

It's the same as doing a[0,1] = 1. In both cases, ndarray's setitem receives two arguments: a tuple for the index (0, 1) and the value 1.

a[[0,1]] = 1

This is a special case of broadcasting. It would usually be written a[0:2] = 1, but you could also slice/mutate other rows for example a[[0,1,3]]. The scalar 1 gets "stretched" across all columns of rows 0 and 1 in the assignment.

Sign up to request clarification or add additional context in comments.

2 Comments

Got it thanks, I also thought of it by another way, writing it down as a answer
The list case could also be written a a[0,1], :]. This makes it clearer, to humans, that the list is selecting two whole rows.
0

I also tried following cases in terminal.

a[[0],[1]] = 1
a[(0),(1)] = 1
a[(0,),(1,)] = 1

All of them are equivalent to a[0,1] = 1. What I understood from this is that, Numpy is expecting N different sequences of integers for indexing N dimensions of array.

Meaning, indexing array as a[[ 0,1 ]] results in a[ [0,1], :].

[0,1] is taken as a one sequence of integers to access first dimension of the array and since no indexing is mentioned for remaining dimension it is taken as:by default which results in a[[0,1],:].

Answer by wim gave me a direction for this thought.

Cheers!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.