The ndarray: NumPy

The ndarray is NumPy's central data structure — a homogeneous, fixed-type, contiguous grid of values that can have any number of dimensions, from a single row of numbers to a multi-dimensional cube of data.

Learn The ndarray: NumPy in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll see exactly what makes the ndarray special, how 1D, 2D, and 3D arrays are structured, why it beats a Python list, and how to read its dtype and dimensions.

The ndarray (short for n-dimensional array ) is a grid of values that are all the same type and laid out in one continuous block of memory. Three properties define it:

Because the layout is so regular, NumPy can run math on the whole array in compiled C — no Python loop required.

Expected output: the array [10 20 30 40] , its type numpy.ndarray , and a single dtype such as int64 .

An ndarray can have any number of dimensions, called axes . The .ndim attribute tells you how many there are: a flat list of numbers is 1D, a table or matrix is 2D, and a stack of matrices is 3D.

Expected output: 1 , 2 , 3 , then the 2×3 matrix. Each extra level of nested lists adds one dimension.

A Python list is flexible — it can hold mixed types and grow freely — but that flexibility costs speed and memory. An ndarray is the opposite: one fixed dtype, contiguous storage, and vectorized math. Notice how building an array from mixed numbers forces a single common dtype.

Expected output: the list keeps an int and a float, but the array prints [1. 2.5 3. ] with dtype float64 because every element must share one type.

You read values out of an ndarray with square-bracket indexing, just like a list. In a 1D array arr[0] is the first element. In a 2D array you give a row and a column with arr[row, col] .

Expected output: 100 , 300 , 400 from the 1D array, then 2 and 6 from the 2D grid using [row, col] .

Replace each ___ so the program builds a 2D array and prints its dimensions and one element.

Expected output: 2 then 3 . (Answers: array , ndim , 2 .)

❌ IndexError: index 4 is out of bounds for axis 0 with size 4

You asked for an index past the end. An array of length 4 has valid indices 0 to 3.

✅ Fix: use indices from 0 to len(arr) - 1 , or use arr[-1] for the last item.

❌ Surprise: my ints turned into floats / strings

An ndarray is homogeneous, so mixed inputs are upcast to one common dtype.

✅ Fix: keep inputs the same type, or set the dtype explicitly with dtype= .

Create a 2D array of test scores, then report its dimensions, dtype, and the value in the middle.

Lesson 2 complete — you understand the ndarray!

You now know that the ndarray is homogeneous, fixed-type, and contiguous, how 1D, 2D, and 3D arrays are structured, and how to read elements, .ndim, and .dtype.

🚀 Up next: Creating Arrays — learn the handy functions that build arrays for you, like zeros, arange, and linspace.

Practice quiz

What does ndarray stand for?

  • n-dimensional array
  • named array
  • nested array
  • numeric array

Answer: n-dimensional array. ndarray is short for n-dimensional array, NumPy's central data structure.

Which word describes an ndarray holding one single dtype for every element?

  • Heterogeneous
  • Homogeneous
  • Dynamic
  • Sparse

Answer: Homogeneous. An ndarray is homogeneous: every element shares one fixed dtype.

What does arr.ndim return for np.array([[[1,2],[3,4]],[[5,6],[7,8]]])?

  • 1
  • 2
  • 3
  • 4

Answer: 3. Three nested bracket levels make it a 3D array, so ndim is 3.

What dtype does np.array([1, 2.5, 3]) have?

  • int64
  • object
  • float64
  • str

Answer: float64. Mixed ints and floats upcast to one common dtype: float64.

How do you read row 1, column 2 of a 2D array named grid?

  • grid(1, 2)

NumPy uses grid[row, col], so grid[1, 2] reads row 1, column 2.

Which attribute gives an ndarray's element data type?

  • arr.dtype
  • arr.type
  • arr.kind
  • arr.format

Answer: arr.dtype. arr.dtype reports the single shared data type of the elements.

How is an ndarray different from a Python list?

  • It can hold any mix of types
  • It is scattered in memory
  • It holds one fixed dtype in contiguous memory
  • It cannot do math

Answer: It holds one fixed dtype in contiguous memory. An ndarray is homogeneous and contiguous, unlike a flexible Python list.

What is arr[-1] for arr = np.array([100, 200, 300, 400])?

  • 100
  • an error
  • 300
  • 400

Answer: 400. Negative indexing counts from the end, so arr[-1] is the last value, 400.

What does .ndim return for a flat 1D array?

  • 1
  • 0
  • the length
  • 2

Answer: 1. A flat list of numbers has a single axis, so ndim is 1.

Why can NumPy run math on a whole array without a Python loop?

  • It rewrites your code
  • Its regular contiguous layout lets compiled C scan it
  • It uses a database
  • It caches results

Answer: Its regular contiguous layout lets compiled C scan it. The fixed-type contiguous layout lets NumPy run the math in compiled C.