DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

How to Convert a List to an Array in Python

Use NumPy’s np.array(my_list) for numerical arrays. Learn how list nesting sets dimensions, when to specify dtype, and how Python’s array.array differs.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a NumPy array, pass the list to np.array(): arr = np.array(values). A flat list becomes a one-dimensional array; nested lists become multidimensional. Python also has a separate built-in array.array type for compact sequences of constrained basic values.

Convert a list to a NumPy array

NumPy’s ndarray is the usual choice for numerical work, especially when you need multidimensional arrays. Install NumPy if needed, then import it and call np.array() with your list:

import numpy as np

values = [1, 2, 3]
arr = np.array(values)

print(arr)

The result is a one-dimensional NumPy array containing the list’s values.

Choose dimensions from the list structure

NumPy uses the nesting of the input list to determine the array’s dimensions. A flat list produces a 1D array, a list of lists produces a 2D array, and deeper nesting produces further dimensions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import numpy as np

one_dimensional = np.array([1, 2, 3])
two_dimensional = np.array([[1, 2], [3, 4]])

The second example has two rows and two columns. For consistent dimensions, nested lists should have matching lengths; irregular nesting may not form the regular multidimensional array you intended.

Control the element type with dtype

Without an explicit type, NumPy infers a dtype from the values. When a list mixes numeric types, NumPy may promote them to a common type: for example, [1, 2, 3.0] becomes an array of floating-point values.

Specify dtype when the representation matters:

arr_float = np.array([1, 2, 3], dtype=float)
arr_int32 = np.array([1, 2, 3], dtype=np.int32)

A constrained dtype may not represent every input value. NumPy’s dtype guide demonstrates that assigning 128 to an int8 value raises an overflow error. Choose a type that can hold the values you expect rather than assuming conversion will preserve out-of-range numbers.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use Python’s built-in array.array for basic values

If you do not need NumPy’s multidimensional numerical arrays, Python’s standard library includes array.array. It stores a sequence of basic values constrained by a one-character type code. For example, 'd' selects double-precision floating-point values:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from array import array

values = [1.0, 2.0, 3.0]
arr = array('d', values)

This is a distinct type from NumPy’s ndarray, not a direct replacement for its multidimensional arrays. Choose between them based on the operation: use NumPy for multidimensional numerical work and its array operations; use array.array when a sequence of a particular basic value type is what you need.

Quick comparison

Type How to create it from a list Best fit
NumPy ndarray np.array(values) Numerical work, multidimensional arrays, and explicit NumPy dtype handling.
Python array.array array('d', values) for double-precision floats; choose a type code for the required basic value type. A sequence of constrained basic values in the standard library.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.