Coding

Array in Python: Lists vs the array Module vs NumPy Arrays

In most Python code, "an array" means a list: numbers = [1, 2, 3]. Python also has a real typed array in the standard library, array.array, and NumPy provides numpy.ndarray for numeric work, so which one you want depends on what is stored in it.

The short version:

  • Use a list by default. It holds anything and has no import.
  • Use array.array when you need a long sequence of one numeric type stored compactly, or raw bytes for a file or socket.
  • Use a NumPy array when you want to do arithmetic on whole collections of numbers, or need rows and columns.

Array in Python with a list

Python has no array literal. Square brackets create a list, and the Python tutorial describes lists as the main container with mutating methods such as append, extend, insert, remove, pop and sort. Indexing starts at 0 and negative indexes count from the end.

numbers = [10, 20, 30]
numbers.append(40)        # [10, 20, 30, 40]
numbers[0] = 15           # [15, 20, 30, 40]
print(numbers[-1])        # 40
print(numbers[1:3])       # [20, 30]
print(len(numbers))       # 4

The Python library reference says lists are "typically used to store collections of homogeneous items", but it is a convention, not a rule. A list accepts mixed types without complaint:

mixed = [1, "two", 3.0, None]

Methods that change a list in place, such as sort(), reverse() and insert(), return None, so numbers = numbers.sort() replaces your list with None. Call numbers.sort() on its own line, or use sorted(numbers) to get a new list. To build a list from a rule, see Python list comprehension. If you need a sequence that cannot change, see what a tuple is in Python.

Two-dimensional arrays with lists

A grid in plain Python is a list of lists:

grid = [[1, 2, 3],
        [4, 5, 6]]
print(grid[1][2])   # 6

Build an empty grid with a comprehension, not with multiplication. [[0] * 3] * 2 repeats a reference to the same inner list, so changing one row changes both. This is the safe form:

rows, cols = 2, 3
grid = [[0] * cols for _ in range(rows)]
grid[0][0] = 9
print(grid)   # [[9, 0, 0], [0, 0, 0]]

The inner [0] * cols is safe because integers are immutable. The problem only appears when the repeated item is itself a list.

The array module: a typed array

The array module in the standard library can, in the documentation's words, "compactly represent an array of basic values: characters, integers, floating-point numbers". Arrays "behave very much like lists, except that the type of objects stored in them is constrained." You pick the type with a one-character type code:

from array import array

values = array('i', [1, 2, 3])
values.append(4)
print(values)             # array('i', [1, 2, 3, 4])
print(values.typecode)    # i
print(values.itemsize)    # bytes per item; depends on the platform

The constructor is array(typecode[, initializer]). The initializer can be a list or other iterable of the right type, or a bytes or bytearray object, which is read as raw machine values.

Common type codes

CodeC typePython typeMinimum size
'b' / 'B'signed / unsigned charint1 byte
'h' / 'H'signed / unsigned shortint2 bytes
'i' / 'I'signed / unsigned intint2 bytes
'l' / 'L'signed / unsigned longint4 bytes
'q' / 'Q'signed / unsigned long longint8 bytes
'f'floatfloat4 bytes
'd'doublefloat8 bytes
'w'Py_UCS4Unicode character4 bytes

The sizes are guaranteed minimums only. The real size depends on the platform and is what itemsize reports. The 'w' code was added in Python 3.13. The older 'u' code is deprecated since 3.3, and the documentation says it will be removed in 3.16 and to migrate to 'w'. (Checked against the docs in November 2026; newer versions add more codes.)

What an array does that a list does not

  • Constrains the type. Slice assignment needs another array with the same type code, and extend() raises TypeError if you give it an array with a different type code.
  • Converts to bytes. values.tobytes() returns the same bytes that tofile() would write to a file, and frombytes() reads them back. This is the usual reason to reach for the module.
  • Stores values compactly. Items are kept as machine values of the chosen C type rather than as separate Python objects.

What it does not do: arithmetic on the whole array. values * 2 repeats the array, just as it does for a list, and there is no element-wise addition. For that you need NumPy.

NumPy arrays

NumPy is a third-party package (pip install numpy), not part of the standard library. Its beginner guide says lists are "excellent, general-purpose containers" that can be heterogeneous, and that NumPy arrays trade that flexibility for speed under three restrictions:

  • All elements must be the same data type.
  • Once created, the total size cannot change.
  • The shape must be rectangular, not jagged.

When those hold, the guide says NumPy "exploits these characteristics to make the array faster, more memory efficient, and more convenient to use than less restrictive data structures".

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6])
print(a.dtype)        # example output: int64 (platform-dependent)
print(a * 2)          # example output: [ 2  4  6  8 10 12]
print(a.reshape(2, 3))

Compare a * 2 with the same expression on a list, which repeats the list instead of doubling each number. The element-wise behaviour is the main reason people switch. For an applied example, see mean in Python with statistics and NumPy.

Which one should you use?

listarray.arraynumpy.ndarray
Where it comes fromBuilt inStandard libraryThird-party package
Mixed typesYesNo, one type codeNo, one dtype
Can growYesYes (append, extend)No, size is fixed once created
Multiple dimensionsNested listsNo, one dimensionYes
Math on the whole collectionNoNoYes
Raw bytes in and outNoYes (tobytes, frombytes)Not covered here

If you are following a tutorial that says "array" and shows square brackets, use a list. If a task mentions binary files or fixed-width numbers, look at array. If it mentions matrices, statistics or large datasets, install NumPy.

Convert between a list and an array

from array import array
import numpy as np

items = [1, 2, 3]

typed = array('i', items)      # list to array.array
back = typed.tolist()          # array.array to list

numpy_array = np.array(items)  # list to NumPy
back_again = numpy_array.tolist()

Conversion copies the data, so editing the new object does not change the original list.

Common mistakes

  • Choosing the wrong type code. 'i' holds integers, so use 'd' or 'f' for floating-point values.
  • Passing a list without a type code. array([1, 2, 3]) is wrong because the first argument must be the type code.
  • Forgetting the import. Write from array import array. With just import array you call array.array('i', ...).
  • Naming your file array.py. It shadows the standard module, and import array then loads your file instead.
  • Using list.pop(0) as a queue. The Python tutorial calls lists not efficient for this and recommends collections.deque.

Frequently asked questions

Does Python have a built-in array type?
Not as a keyword or built-in name. Python has the built-in list, which most people use as an array. The standard library also has the array module, which stores numbers of one fixed type compactly, and NumPy (a third-party package) provides n-dimensional arrays.
What is the difference between a list and an array in Python?
A list can hold items of different types and is always available. An array.array holds only one numeric (or character) type chosen by a type code, stored compactly. A NumPy array also holds one type, has a fixed size once created, and supports fast math on whole arrays.
How do I create an empty array in Python?
For a list, use [] or list(). For the array module, use array('i') with the type code you need, for example from array import array; values = array('d'). For NumPy, use np.empty(0) or np.array([]).
Can an array.array hold strings?
Only single characters, using the 'u' or 'w' type code. The 'u' code is deprecated since Python 3.3 and the documentation says it will be removed in 3.16, so use 'w' (added in 3.13) on new code. For ordinary strings, use a list.
How do I convert a list to an array?
For the array module, pass the list to the constructor with a type code: array('i', [1, 2, 3]). For NumPy, call np.array([1, 2, 3]). To go back, call list(your_array) or your_array.tolist().

Sources