Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 49 ยท Getting Started

Your First Arrays

Build NumPy arrays from lists and nested lists, then read their ndim, shape, size, dtype, itemsize and nbytes. Learn how 1D, 2D and 3D arrays print.

The simplest way to make a NumPy array is to hand a Python list to np.array(). From that moment you have an object with a shape, a dtype and a handful of attributes that describe it. Reading those attributes fluently is the first real NumPy skill, because nearly every error message and every operation talks about shapes and types.

This lesson shows how lists become arrays, how nesting creates rows and columns, and what each attribute tells you. If you have not seen NumPy at all yet, start with What Is NumPy?.

From a list to an array

np.array() takes a sequence and returns an ndarray. A flat list gives a 1D array, which is NumPy's version of a vector.

Python Lists become arrays

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Because an array has one dtype for all elements, NumPy picks a type that fits every value. One float in the list turns the whole array into floats. A string among numbers turns everything into strings, which is rarely what you want, so keep your data clean.

Notice the printed form: [3 1 4 1 5] has no commas. That is str(), designed to look like a mathematical vector. The repr() form you see in an interactive session, array([3, 1, 4, 1, 5]), is designed to be pasted back into code.

Nested lists make rows and columns

A list of lists becomes a 2D array. Each inner list is one row, and all rows must have the same length.

graph LR
    L["[[1, 2, 3],<br/>[4, 5, 6]]"] --> A["np.array(...)"]
    A --> S["shape (2, 3)<br/>2 rows, 3 columns"]

A list of two lists with three items each becomes a 2 by 3 array.

Python A 2D array and its shape

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the shape (2, 3) as "2 rows by 3 columns". The shape is a tuple with one number per axis (dimension). Axis 0 runs down the rows, axis 1 runs across the columns.

If the rows have different lengths, NumPy 2 refuses to guess and raises a ValueError about an "inhomogeneous shape". That is a feature: ragged data cannot be an ordinary array.

The six attributes to know

Every array carries these:

Attribute Meaning
ndim Number of dimensions (axes)
shape Tuple with the length of each axis
size Total number of elements (the product of the shape)
dtype The data type of each element
itemsize Bytes used by one element
nbytes Total bytes of the data (size * itemsize)
Python Reading every attribute

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Note that len(a) returns only the length of the first axis (3 rows here), not the total count. Use a.size for the total. A 64-bit float takes 8 bytes, so twelve of them need 96 bytes.

1D, 2D and 3D

A 3D array is a list of 2D arrays. Think of a stack of sheets, a short video (frames of images), or a color image (height by width by three channels).

graph TD
    A3["3D array<br/>shape (2, 2, 3)"] --> P0["page 0<br/>2 x 3"]
    A3 --> P1["page 1<br/>2 x 3"]
    P0 --> R0["rows of 3 numbers"]
    P1 --> R1["rows of 3 numbers"]

Shape (2, 2, 3) means 2 pages, each with 2 rows of 3 numbers.

Python One, two and three dimensions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Printing a 3D array shows the pages separated by blank lines, so you can read the structure at a glance. The shape tuple lists axes from outermost to innermost: pages, rows, columns.

Scalars and empty arrays

A single number can be an array too, with zero dimensions and an empty shape (). An empty list gives an array of size 0, whose default dtype is float64.

Python Zero dimensions and empty arrays

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The shapes (0,) and (1, 0) look similar but differ, and mixing them up is a classic source of confusing bugs. You will see more of this when you learn reshaping.

Choosing a dtype yourself

If you do not pass a dtype, NumPy guesses from the data. You can override the guess with the dtype argument:

Python
import numpy as np

a = np.array([1, 2, 3], dtype=np.float32)
b = np.array([1, 2, 3], dtype="int8")

Data Types (dtype) explains the options and how to choose.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the shape of np.array([[1, 2, 3], [4, 5, 6]])?

    There are two inner lists (rows) with three numbers each, so the shape is (2, 3).
  2. 2. What dtype does np.array([1, 2.5, 3]) get?

    All elements share one dtype. A float among integers upgrades the whole array to float64.
  3. 3. For an array of shape (4, 5), what do len(a) and a.size return?

    len() reports the length of the first axis (4). size is the total number of elements, 4 * 5 = 20.
  4. 4. A float64 array has 10 elements. What is its nbytes?

    A float64 element is 8 bytes (itemsize), so nbytes is 10 * 8 = 80.

Summary

  • np.array(list) builds an ndarray; nested lists create 2D and 3D arrays.
  • All elements share one dtype, and NumPy chooses one that fits every value.
  • ndim, shape, size, dtype, itemsize and nbytes describe any array.
  • shape is a tuple from the outermost axis to the innermost; len() gives only the first axis.
  • Ragged nested lists cannot form a regular array.

Next: Creating Arrays