Skip to main content
v2026.11,858 entries · CC-BY 4.0

NumPy Array: Create from a List, Convert Back and Read Shape

Create a NumPy array from a list with np.array, convert it back with tolist, and read shape, ndim, size and dtype, with the list() versus tolist() difference.

Written and maintained by CASRAI Editorial Board

Last updated

A NumPy array is created with numpy.array(): pass it a list (or a list of lists) and it returns an ndarray, NumPy’s homogeneous multidimensional array. To go the other way, ndarray.tolist() returns a nested list of ordinary Python numbers. This guide covers both directions, shows how to read an array’s shape and type, and explains the one difference that trips people up: list(arr) and arr.tolist() do not return the same thing. The details come from the official NumPy documentation, linked under Sources.

What a NumPy array is used for in research

The NumPy quickstart describes the main object as “the homogeneous multidimensional array”: a table of elements, usually numbers, all of the same type, indexed by a tuple of non-negative integers. Turning a Python list into an array is typically the first step before the numerical work shown in guides such as ANOVA in Python with statsmodels and train test split in Python. For the broader practice of keeping such scripts repeatable, see computational reproducibility.

Install NumPy and import it

The NumPy install page gives two routes. With pip: pip install numpy. With conda, create and activate an environment, then install:

conda create -n my-env
conda activate my-env
conda install numpy

The conda route uses the defaults or conda-forge channel. If you want the environment to be portable, see sharing a reproducible conda environment. By convention NumPy is imported under the short name np, and you can confirm what is installed by printing the version:

import numpy as np
print(np.__version__)

Convert a Python list to a NumPy array (list to array numpy)

Pass the list as the single first argument to np.array(). The array type is deduced from the type of the elements in the sequence.

  1. Import NumPy: import numpy as np.
  2. Pass one list: np.array([1, 2, 3]) returns array([1, 2, 3]).
  3. Mix integers and floats and NumPy promotes to a type that can hold every value: np.array([1, 2, 3.0]) returns array([ 1., 2., 3.]).
  4. Pass a list of lists for two dimensions: np.array([[1, 2], [3, 4]]) returns a 2 by 2 array. The documentation states that array transforms sequences of sequences into two-dimensional arrays, sequences of sequences of sequences into three-dimensional arrays, and so on. Tuples inside a list work the same way: np.array([(1.5, 2, 3), (4, 5, 6)]) gives a 2 by 3 array of floats.

Parameters worth knowing

The signature in the reference page is numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, ndmax=0, like=None), and it returns an ndarray. The ones you will use most:

  • dtype: if you do not give one, NumPy tries to use a default dtype that can represent the values, applying promotion rules when necessary. To force a type, for example complex: np.array([1, 2, 3], dtype=np.complex128) returns array([ 1.+0.j, 2.+0.j, 3.+0.j]).
  • ndmin: the minimum number of dimensions, with ones prepended to the shape as needed. np.array([1, 2, 3], ndmin=2) returns array([[1, 2, 3]]). The default is 0.
  • copy: the default is True. None copies only when needed, and False raises a ValueError when a copy cannot be avoided.
  • subok: default False. ndmax is marked “New in 2.4.0” and like “New in 1.20.0” in the reference page, so older installs may not accept them.

If you pass a scalar instead of a sequence, you get a 0-dimensional array containing it, which matters again in the conversion section below.

Read the array: shape, ndim, size and dtype

Four attributes describe what you created. The quickstart defines them as follows, using np.arange(15).reshape(3, 5) as its example.

Attribute Meaning (NumPy quickstart) For np.arange(15).reshape(3, 5)
shape Tuple giving the size of the array in each dimension (3, 5)
ndim Number of axes (dimensions) 2
size Total number of elements, the product of the elements of shape 15
dtype The type of the elements an integer type

The quickstart shows the deduced type for a list of integers and a list of floats: np.array([2, 3, 4]).dtype is, for example, dtype('int64') and np.array([1.2, 3.5, 5.1]).dtype is dtype('float64'). The integer width can vary by platform, so do not hard-code it in tests.

Convert a NumPy array to a list (numpy array to list)

Call .tolist() on the array. According to the reference page, ndarray.tolist() takes no parameters and returns the array “as an a.ndim-levels deep nested list of Python scalars.” It returns a copy, and each item is converted to the nearest built-in Python type.

  1. Create an array: a = np.array([[1, 2], [3, 4]]).
  2. Convert it: a.tolist() returns [[1, 2], [3, 4]], a list of lists of Python int values.

list(arr) versus arr.tolist()

Both give you a Python list, but the contents differ. The tolist documentation compares them directly.

Case list(a) a.tolist()
1-D: a = np.uint32([1, 2]) [np.uint32(1), np.uint32(2)], items are of type numpy.uint32 [1, 2], items are Python int
2-D: [[1, 2], [3, 4]] [array([1, 2]), array([3, 4])], a list of arrays [[1, 2], [3, 4]], a list of lists
0-D: np.array(1) raises TypeError: iteration over a 0-d array returns 1, a plain Python scalar and not a list

The practical rule that follows: use tolist() when you need pure Python objects (for example before handing data to code that expects built-in types), and use list(a) only when you want to iterate over the first axis and are happy to receive NumPy scalars or sub-arrays.

Round trip: list, array, list

The reference page notes that “The array may be recreated via a = np.array(a.tolist()), although this may sometimes lose precision.” Treat the round trip as safe for display and hand-off, and compare values explicitly if exact precision matters.

Troubleshooting

TypeError: array() takes from 1 to 2 positional arguments

The quickstart lists this as a common error. np.array(1, 2, 3, 4) raises TypeError: array() takes from 1 to 2 positional arguments but 4 were given, because array expects one sequence, not several numbers. The fix is to wrap them in a list: np.array([1, 2, 3, 4]).

ValueError with copy=False

With copy=False, NumPy raises a ValueError if a copy cannot be avoided. Use copy=None to copy only when needed.

TypeError: iteration over a 0-d array

This appears when you call list() on a 0-dimensional array, such as np.array(1). Use .tolist(), which returns the scalar.

Unexpected dtype

If the dtype is not what you wanted, state it: np.array(data, dtype=...). Without it, NumPy chooses a type that can represent all values, so one float in an otherwise integer list makes the whole array float.

Related calls in NumPy and Python

The calls below are the ones used in this guide.

Task Call Returns
Build an array from a list or nested list np.array(seq) ndarray, dimensions follow the nesting
Build a numbered array and give it a shape np.arange(15).reshape(3, 5) ndarray with shape (3, 5)
Array to nested list of Python scalars a.tolist() list, a.ndim levels deep
Iterate the first axis into a list list(a) list of NumPy scalars (1-D) or sub-arrays (n-D)

For other array-building and array-selection calls, see NumPy linspace for evenly spaced values and NumPy where for conditional selection. NumPy itself is open source software.

Frequently asked questions

How do I make a NumPy array in Python?

Import NumPy as np and call np.array() with a single list, for example np.array([1, 2, 3]). Nested lists give a multidimensional array.

What is the difference between a list and a NumPy array?

Per the NumPy quickstart, a NumPy array is homogeneous: all elements share one type, and it is indexed by a tuple of non-negative integers. Its type is deduced from the elements you pass in.

How do I convert a NumPy array to a list?

Use a.tolist(). It returns a nested list of Python scalars, with one level of nesting per dimension.

Why does list(arr) give NumPy types but arr.tolist() gives Python types?

list(arr) iterates the array and keeps each element as a NumPy object, while tolist() converts every item to the nearest built-in Python type.

Does converting a list to an array and back lose data?

The NumPy documentation says recreating an array from a.tolist() may sometimes lose precision, so verify values when exactness matters.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about NumPy Array: Create from a List, Convert Back and Read Shape

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.