1

Looking at a numpy array, i find two methods to get an element from the answers given to this question: How to call an element in a numpy array?.

There are the two methods: a[i][j] or a[i,j]

Here is a minimum reproducable example:

import numpy as np
a = np.arange(15).reshape(3, 5)
'''
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
'''


print(a[1][1])  # returns 6
print(a[1,1])   # return 6

Both appear to give the same result, so is one method better than the other ?

2
  • 1
    a[1][1] is more work because numpy has to create an array view a[1] only for accessing a single element from it before the view object is thrown away (also work). Commented Aug 31, 2022 at 10:24
  • While for accessing an element at a specific position in a N-dim array, the single getitem() call is the preferred one, there are cases where multiple calls to getitem() are difficult to express otherwise (e.g. when they are both a list of indices, or slices). Commented Aug 31, 2022 at 10:37

3 Answers 3

4

You have the answer in the docs

Note that if one indexes a multidimensional array with fewer indices than dimensions, one gets a subdimensional array. For example:

x[0]
array([0, 1, 2, 3, 4])

That is, each index specified selects the array corresponding to the rest of the dimensions selected. In the above example, choosing 0 means that the remaining dimension of length 5 is being left unspecified, and that what is returned is an array of that dimensionality and size. It must be noted that the returned array is a view, i.e., it is not a copy of the original, but points to the same values in memory as does the original array. In this case, the 1-D array at the first position (0) is returned. So using a single index on the returned array, results in a single element being returned. That is:

x[0][2]
2

So note that x[0, 2] == x[0][2] though the second case is more inefficient as a new temporary array is created after the first index that is subsequently indexed by 2.

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

Comments

3

Single element indexing

Single element indexing works exactly like that for other standard Python sequences. It is 0-based, and accepts negative indices for indexing from the end of the array.

x = np.arange(10)
x[2]
2
x[-2]
8

It is not necessary to separate each dimension’s index into its own set of square brackets.

x.shape = (2, 5)  # now x is 2-dimensional
x[1, 3]
8
x[1, -1]
9

Note that if one indexes a multidimensional array with fewer indices than dimensions, one gets a subdimensional array. For example:

x[0]
array([0, 1, 2, 3, 4])

That is, each index specified selects the array corresponding to the rest of the dimensions selected. In the above example, choosing 0 means that the remaining dimension of length 5 is being left unspecified, and that what is returned is an array of that dimensionality and size. It must be noted that the returned array is a view, i.e., it is not a copy of the original, but points to the same values in memory as does the original array. In this case, the 1-D array at the first position (0) is returned. So using a single index on the returned array, results in a single element being returned. That is:

x[0][2]
2

So note that x[0, 2] == x[0][2] though the second case is more inefficient as a new temporary array is created after the first index that is subsequently indexed by 2.

Note

NumPy uses C-order indexing. That means that the last index usually represents the most rapidly changing memory location, unlike Fortran or IDL, where the first index represents the most rapidly changing location in memory. This difference represents a great potential for confusion.

source

Comments

2
a[1][1]

Returns the slice a[1] and then indexes that slice at [1]

a[1] -> array([5, 6, 7, 8, 9])
array([5, 6, 7, 8, 9])[1] -> 6

Whereas

a[1, 1]

Does the same end-result calculation but all within the numpy API in C, so is faster:

a[1][1]
360 ns ± 28.7 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

a[1, 1]
191 ns ± 2.76 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

4 Comments

okay, so according to the above the answer is without a doubt a[i,j]. Is there a case where a[i][j] is preferred in that respect?
@D.L. I cant think off the top of my head any time I would use a[i][j] over a[i, j]
So this answer shows a 2x gain (approximately). question: why did you use 1 mill loops vs 10 mill loops in the speed calculation ? (logical to test on the same size).
@D.L this is what timeit uses by default -- tweak the number of loops to give a reasonable estimate within reasonable time.

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.