Array Attributes
Read an array's metadata — shape, ndim, size, dtype, itemsize and nbytes — and understand how they relate to each other.
Before a captain sets a field he reads the pitch. Before you compute on an array, read its attributes: how many dimensions it has, how many values, what type, how much memory. They answer "what exactly am I dealing with?" — and most NumPy errors (shape mismatches, wrong dtypes) are solved by printing them.
Definition
Array attributes are read-only pieces of metadata stored with every ndarray that describe its structure:
shape,ndim,size,dtype,itemsizeandnbytes. They are accessed without parentheses (arr.shape, notarr.shape()) because they are stored values, not methods that do work.
- Metadata — "data about the data": it describes the array, not the scores inside it.
- Attribute vs method — an attribute is a value (
arr.shape); a method is a function you call (arr.sum()). - Read-only (mostly) — you normally don't assign to them; to change a shape you use
reshape(covered later).
Syntax
python
arr.shape # tuple: size of each dimension
arr.ndim # int: number of dimensions (= len(arr.shape))
arr.size # int: total number of elements (= product of shape)
arr.dtype # dtype: data type of every element
arr.itemsize # int: bytes per element
arr.nbytes # int: total bytes of data (= size * itemsize)| Attribute | What it tells you | Type | 1D example (14,) | 2D example (3, 8) |
|---|---|---|---|---|
.shape | Length of every dimension | tuple | (14,) | (3, 8) |
.ndim | Number of dimensions (axes) | int | 1 | 2 |
.size | Total number of elements | int | 14 | 24 |
.dtype | Element data type | dtype | int64 | int64 |
.itemsize | Bytes per element | int | 8 | 8 |
.nbytes | Total bytes of the data | int | 112 | 192 |
Example — attributes of a 1D array
python
import numpy as np
virat_runs = np.array([72, 45, 88, 31, 64, 0, 103, 52, 19, 77, 41, 93, 28, 66])
print("shape :", virat_runs.shape)
print("ndim :", virat_runs.ndim)
print("size :", virat_runs.size)
print("dtype :", virat_runs.dtype)
print("itemsize:", virat_runs.itemsize)
print("nbytes :", virat_runs.nbytes)text
shape : (14,)
ndim : 1
size : 14
dtype : int64
itemsize: 8
nbytes : 112How it works: 14 matches in one row → shape (14,) (the comma makes it a one-element tuple), 1 dimension, 14 elements. Each int64 takes 8 bytes, so 14 × 8 = 112 bytes.
Example — attributes of a 2D array
python
import numpy as np
match_scores = np.array([
[72, 45, 88, 31, 64, 0, 103, 52],
[56, 0, 74, 88, 32, 45, 61, 17],
[45, 32, 0, 67, 18, 54, 78, 22],
])
print("shape :", match_scores.shape)
print("ndim :", match_scores.ndim)
print("size :", match_scores.size)
print("dtype :", match_scores.dtype)
print("itemsize:", match_scores.itemsize)
print("nbytes :", match_scores.nbytes)
print("rows :", match_scores.shape[0])
print("cols :", match_scores.shape[1])
print(len(match_scores))text
shape : (3, 8)
ndim : 2
size : 24
dtype : int64
itemsize: 8
nbytes : 192
rows : 3
cols : 8
3How it works: 3 players (rows) × 8 matches (columns). shape[0] is the number of rows and shape[1] the number of columns — index the shape tuple like any tuple. size = 3 × 8 = 24, nbytes = 24 × 8 = 192. Note that len(arr) gives only the first dimension (3), not the total count — use .size for that.
How the attributes relate
text
shape = (3, 8)
ndim = len(shape) = 2
size = 3 * 8 = 24
nbytes = size * itemsize = 24*8 = 192The shape is the most important attribute: its length is ndim, its product is size.
Example — edge cases worth knowing
python
import numpy as np
a = np.arange(6)
print(a.shape)
try:
a.shape()
except TypeError as e:
print("TypeError:", e)
s = np.array(42)
print(s.shape, s.ndim, s.size)
row = np.array([[1, 2, 3]])
col = np.array([[1], [2], [3]])
print(row.shape, col.shape, np.array([1,2,3]).shape)
f32 = np.zeros(1000, dtype=np.float32)
f64 = np.zeros(1000)
print(f32.nbytes, f64.nbytes)
print(np.shape([[1, 2], [3, 4]]), np.ndim([[1,2],[3,4]]), np.size([[1,2],[3,4]]))text
(6,)
TypeError: 'tuple' object is not callable
() 0 1
(1, 3) (3, 1) (3,)
4000 8000
(2, 2) 2 4How it works:
- Calling
a.shape()fails —shapeis a tuple, not a function. - A 0-D array (a single scalar) has shape
(), ndim 0, size 1. (3,),(1, 3)and(3, 1)all hold 3 values but are different shapes: a 1D array, a 2D row, and a 2D column. This matters for broadcasting and matrix maths.- Switching to
float32halves memory (4000 vs 8000 bytes for 1000 values). - The functions
np.shape(),np.ndim(),np.size()also work on plain lists.
Common mistakes
- ❌
arr.shape()/arr.size()with parentheses. ✅arr.shape,arr.size— they are attributes. - ❌ Using
len(arr)to count all elements of a 2D array. ✅arr.size;lengives onlyshape[0]. - ❌ Treating
(3,)and(3, 1)as the same. ✅ Check.shapewhenever an operation behaves unexpectedly. - ❌ Confusing
itemsize(bytes per element) withnbytes(total bytes). ✅nbytes = size * itemsize.
Interview tip
Q: An array has shape (4, 5) and dtype float32. What are its ndim, size and nbytes?
A: ndim = 2 (two numbers in the shape), size = 4 × 5 = 20, itemsize = 4 bytes, so nbytes = 20 × 4 = 80.
Key points to remember
- Attributes are metadata read without parentheses: arr.shape, arr.ndim, arr.size, arr.dtype, arr.itemsize, arr.nbytes.
- shape is a tuple with the length of every dimension; shape[0] = rows, shape[1] = columns for 2D.
- ndim = len(shape) and size = product of the shape.
- itemsize is bytes per element; nbytes = size × itemsize.
- len(arr) returns only the first dimension, not the total number of elements.
- (3,), (1, 3) and (3, 1) all hold 3 values but are different shapes — 1D, row, column.
Try it yourself
All six attributes of a team table
Same values, different shapes
dtype changes memory, not size
Practice: inspect a bowling table
Revision Flashcards
Tap a card to flip it and see the answer.
