Python Data Structures
Python Arrays
Python does not have a dedicated array type built into the core language the way C does — instead you choose between lists, the array module, and NumPy depending on your needs.
Lists Are Python’s Default "Array"
For most beginner and everyday tasks, a regular list already behaves like a dynamic array: it grows automatically and can be indexed, sliced, and looped over. Most Python code never needs anything more specialised.
scores = [85, 92, 78, 90]
scores.append(88)
print(scores[2]) # 78The array Module
The built-in array module gives you a true fixed-type array. Every element must be the same numeric type, declared with a one-letter typecode, which saves memory compared to a list of the same size.
import array
nums = array.array("i", [1, 2, 3, 4]) # 'i' = signed int
nums.append(5)
print(nums) # array('i', [1, 2, 3, 4, 5])
print(nums[0]) # 1Common Typecodes
| Typecode | C Type | Python Type |
|---|---|---|
| 'i' | signed int | int |
| 'f' | float | float |
| 'd' | double | float |
| 'u' | wchar_t | Unicode character |
NumPy Arrays
For serious numerical work — data analysis, machine learning, image processing — the third-party NumPy library’s ndarray is the standard choice. It supports fast vectorised math, multiple dimensions, and a huge ecosystem of tools.
import numpy as np
grid = np.array([[1, 2], [3, 4]])
print(grid.shape) # (2, 2)
print(grid * 2) # element-wise multiplicationpip install numpyWhich One Should You Use?
- List — default choice for everyday Python code, mixed types, small to medium data.
- array module — large collections of one numeric type, when you need lower memory use and have no extra dependencies.
- NumPy array — numeric/scientific computing, matrices, and any project already using data-analysis libraries.
When people say "Python array" in casual conversation they usually mean a list — the array module and NumPy are more specialised tools you reach for as your needs grow.
