Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Numpy array dimensions

I'm as of now attempting to learn Numpy and Python. Given the following array:

import numpy as np
a = np.array([[1,2],[1,2]])


Is there a function that returns the dimensions of a (e.g.a is a 2 by 2 array)?

size() returns 4 and that doesn't help very much.
by

3 Answers

aashaykumar

import numpy as np
>>> np.shape(a)
(2,2)

Also works if the input is not a numpy array but a list of lists

>>> a = [[1,2],[1,2]]
>>> np.shape(a)
(2,2)

Or a tuple of tuples

>>> a = ((1,2),(1,2))
>>> np.shape(a)
(2,2)
sandhya6gczb
It is .shape:

ndarray.shape

Tuple of array dimensions.

Thus:

>>> a.shape
(2, 2)
sandhya6gczb
It is .shape:

ndarray.shape

Tuple of array dimensions.

Thus:

>>> a.shape
(2, 2)

Login / Signup to Answer the Question.