October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Arrays in Python: A Practical Guide to Lists, array.array, and NumPy

Python has lists, standard-library array.array, and NumPy ndarray. Learn how they differ and how to create, inspect, index, and safely slice NumPy arrays.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Python, “array” can mean three different things: a built-in list, the standard-library array.array, or NumPy’s ndarray. Use a list for general-purpose sequences, array.array for compact one-dimensional sequences of constrained basic values, and NumPy when you need multidimensional arrays and array-oriented numerical operations.

Which kind of array does Python mean?

The word is not specific enough to identify one Python type. These structures differ in where they come from, what they can hold, and whether they support multidimensional numerical work.

Structure Where it comes from Element types Multidimensional shape Best suited to
list Built into Python Can contain values of different types No native multidimensional shape; nested lists can represent rows and columns General-purpose sequences and mixed values
array.array Python standard library Constrained to a basic value type selected by a type code One-dimensional Mutable one-dimensional sequences of basic values when its narrower feature set is enough
NumPy ndarray External package; NumPy is not part of Python’s standard library Homogeneous: elements use the array’s dtype Native support for one or more dimensions Numerical work with array-oriented operations

NumPy’s documentation distinguishes its ndarray from the standard-library array.array, which handles one-dimensional arrays with fewer features. See the NumPy v2.5 quickstart, the ndarray reference, and Python’s array module documentation.

How do I create an array in Python?

For NumPy examples below, first import the package as np:

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

The constructor numpy.array(object, dtype=...) builds an array from an object such as a Python sequence. A flat sequence creates a one-dimensional array; nested sequences can create arrays with more dimensions. The optional dtype argument specifies the element type. See the numpy.array reference and array creation guide.

Make a one-dimensional array from a list

values = np.array([10, 20, 30, 40])
print(values)
print(values.shape)  # (4,)
print(values.ndim)   # 1
print(values.size)   # 4
print(values.dtype)  # NumPy's inferred element type

Here, the input is a Python list, but values is a NumPy array. NumPy chooses a dtype based on the input unless you specify one.

Make a two-dimensional array from nested lists

grid = np.array([[1, 2, 3],
                 [4, 5, 6]])

print(grid.shape)  # (2, 3)
print(grid.ndim)   # 2
print(grid.size)   # 6
print(grid.dtype)

The shape (2, 3) means two entries along the first axis (rows) and three along the second (columns). The general creation guide also demonstrates building arrays from nested sequences with more dimensions.

Use common numeric constructors

NumPy also provides constructors for sequences and arrays initialized with repeated values. For example:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sequence = np.arange(5)          # [0, 1, 2, 3, 4]
zeros = np.zeros((2, 3))         # 2 rows by 3 columns of zeros
ones = np.ones((2, 2), dtype=int)  # 2 by 2 array of integers

These functions are useful when the values follow a range or an initial pattern rather than coming from an existing list.

What do shape, ndim, size, and dtype tell me?

  • shape is a tuple giving the length along each dimension. A two-row, three-column array has shape (2, 3).
  • ndim is the number of axes (dimensions). A flat array has one axis; a matrix-like array has two.
  • size is the total number of elements across all dimensions.
  • dtype describes the type used for the array’s elements.

These attributes answer different questions: shape describes layout, ndim counts axes, size counts values, and dtype identifies their representation. For the formal definitions, see the NumPy ndarray reference.

How do I access or slice a NumPy array?

NumPy uses familiar bracket notation. For a two-dimensional array, give row and column indices separated by a comma:

grid = np.array([[1, 2, 3],
                 [4, 5, 6]])

print(grid[1, 2])  # 6: row index 1, column index 2
print(grid[0])     # first row

Indices start at zero, so grid[1, 2] selects the second row’s third element. A slice selects a range or part of an array:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
first_row = grid[0, :]
second_column = grid[:, 1]
print(second_column)  # [2, 5]

Important: a slice may share the original data

NumPy slices can be views rather than independent copies. Changing a value through a view can therefore change the source array:

grid = np.array([[1, 2, 3],
                 [4, 5, 6]])
second_column = grid[:, 1]
second_column[0] = 99

print(grid)
# [[ 1 99  3]
#  [ 4  5  6]]

If you need independent values, explicitly copy the slice:

second_column_copy = grid[:, 1].copy()

The ndarray reference documents tuple-based indexing and the shared-data behavior of views.

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

When should I choose array.array instead?

Use array.array when you want a mutable, one-dimensional sequence whose values are constrained by a basic type code, and you do not need NumPy’s multidimensional or numerical feature set. It is in Python’s standard library:

Free tools Windows power users keep installed

One-click scans. No signup required.

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

values = array('i', [10, 20, 30])
values.append(40)
print(values)

The type code 'i' selects a signed integer type. Type codes identify C-compatible types; the exact size of some codes can depend on the platform, so do not assume every code has a universal byte layout. Consult the Python array documentation for the codes and platform details.

Compatibility note for Python 3.14

In the Python 3.14.7 documentation, type code 'u' is deprecated and scheduled for removal in Python 3.16, while 'w' was added in Python 3.13. Code using either should be checked against the Python versions it needs to support.

What should I use for a “2D array”?

A nested Python list can hold rows, but it remains a list of lists. Choose NumPy’s ndarray when your task needs a multidimensional array with a defined shape and array-oriented numerical operations. Choose nested lists when ordinary sequence behavior is sufficient and you do not need those array features. The standard-library array.array is one-dimensional, so it is not a native 2D-array alternative.

Which NumPy version do these references describe?

The linked NumPy manual identifies itself as version 2.5, with a release date of June 28, 2026. Examples here use broadly established constructor, attribute, indexing, and slicing forms; consult the documentation for the NumPy version installed in your environment if version-specific behavior matters. See the NumPy v2.5 Manual.

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

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 *

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
PC Slower Than It Used to Be?Free scan - under a minute
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.