Reputation: 8370
I'm well aware that there are differences between lists and tuples and that tuples aren't just constant lists, but there are few examples where the two are actually treated differently by the code (as opposed to by a coding convention), so I (sloppily) have used them interchangeably.
Then I came across a case where they give totally different behavior:
>>> import numpy as np
>>> a = np.arange(9).reshape(3,3)
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> idx = (1,1)
>>> a[idx]
4
>>> idx = [1,1]
>>> a[idx]
array([[3, 4, 5],
[3, 4, 5]])
can someone explain what's going on here? More importantly, where else does this pitfall appear in scipy?
Upvotes: 6
Views: 270
Reputation: 63737
You are getting a different behavior because, in numpy, three types of indexing are supported
Using tuple for indexing is just equivalent to a parameter list, which suffixes as a Basic Slicing, where-as using a non-tuple like list results in Advanced Indexing.
Also remember, from the documentation
Advanced indexing is triggered when the selection object, obj, is a non-tuple sequence object, an ndarray (of data type integer or bool), or a tuple with at least one sequence object or ndarray (of data type integer or bool). There are two types of advanced indexing: integer and Boolean.
Advanced indexing always returns a copy of the data (contrast with basic slicing that returns a view).
And moreover, from the same documentation
In Python, x[(exp1, exp2, ..., expN)] is equivalent to x[exp1, exp2, ..., expN]; the latter is just syntactic sugar for the former.
Upvotes: 4