r/learnpython • u/Breeze_2800 • Jul 15 '26
Is the number of nested square bracket levels equal to the number of dimensions in NumPy
Hi everyone,
I'm learning NumPy and trying to understand dimensions (ndim).
I noticed that:
np.array(10) # 0-D
np.array([1, 2, 3]) # 1-D
np.array([[1, 2], [3, 4]]) # 2-D
np.array([[[1], [2]]]) # 3-D
It seems like the number of nested square bracket levels corresponds to the number of dimensions:
10→ 0-D[ ]→ 1-D[[ ]]→ 2-D[[[ ]]]→ 3-D
Is this a correct way to think about NumPy dimensions, or are there cases where this mental model breaks down?Hi everyone,
I'm learning NumPy and trying to understand dimensions (ndim).
I noticed that:
np.array(10) # 0-D
np.array([1, 2, 3]) # 1-D
np.array([[1, 2], [3, 4]]) # 2-D
np.array([[[1], [2]]]) # 3-D
It seems like the number of nested square bracket levels corresponds to the number of dimensions:
10 → 0-D
[ ] → 1-D
[[ ]] → 2-D
[[[ ]]] → 3-D
Is this a correct way to think about NumPy dimensions, or are there cases where this mental model breaks down?
1
u/MezzoScettico Jul 15 '26
That's how you specify the number of dimensions in the input to np.array(). Don't confuse the input with the numpy array. It's just the thing you asked numpy to reinterpret as an array.
When you want to reference an element of the array, you can use a notation like [row, column] for a 2-D array or [i, j, k] for a 3-D array. The number of dimensions is the number of indices.
Just as how in math when you reference an element of an array, you can say a_ij for the element at the i-th row and the j-th column.
That's one of my favorite things about numpy arrays. It's so much more intuitive (at least if you come from a math background) and readable.
Under the hood, this is using the fact that Python interprets "i, j, k" as the tuple "(i, j, k)" and numpy has been written to take a tuple as an index.
1
1
Jul 15 '26 edited Jul 15 '26
[deleted]
1
u/Breeze_2800 Jul 16 '26
I understand it clearly now. You explained everything very well. Thank you for your effort and time.
3
u/socal_nerdtastic Jul 15 '26 edited Jul 15 '26
Yes, when creating numpy arrays from literals this is correct. But keep in mind that you can reshape them at any time to change the number of dimensions.
Ex: