Coming from a Lists background in Python and that of programming languages like C++/Java, one is used to the notation of extracting elements using a[i][j]
approach.
The main difference is that a[i][j]
first creates a view onto a[i]
and then indexes into that view. On the other hand, a[i,j]
indexes directly into a
, making it faster:
In [9]: a = np.random.rand(1000,1000)
In [10]: %timeit a[123][456]
1000000 loops, best of 3: 586 ns per loop
In [11]: %timeit a[123,456]
1000000 loops, best of 3: 234 ns per loop
For this reason, I'd prefer the latter.