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?
The key difference between the tuples and lists is that while the tuples are immutable objects the lists are mutable. This means that tuples cannot be changed while the lists can be modified.
One of the most important differences between list and tuple is that list is mutable, whereas a tuple is immutable. This means that lists can be changed, and tuples cannot be changed. So, some operations can work on lists, but not on tuples.
A string is a sequence of unicode characters with quotation marks on either side of the sequence. A tuple is an ordered sequence of objects or characters separated by commas with parentheses on either side of the sequence.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With