1/128
Arrays vs Lists: Why NumPy? · Page 1 of 2

The Power of Vectorization

20 min Intermediate

Numerical Python (NumPy)

What is NumPy?

NumPy (Numerical Python) is the foundational scientific computing library for Python. At its core it provides the ndarray — an N-dimensional array data structure that stores homogeneous numerical data in a single contiguous block of memory. Unlike Python lists, which store pointers to objects scattered across the heap, NumPy arrays pack raw numerical values side-by-side, letting the CPU process many elements per clock cycle through hardware-level vectorization.

Put simply: NumPy lets you do math on entire arrays at once, without writing loops — and does it in C under the hood, making it 10–100× faster than pure Python.

Why Not Just Use Lists?

Python lists are flexible, but slow for math because they store pointers to objects. NumPy arrays store data in contiguous C-level memory blocks.

The Vectorization Difference

# Python List approach (SLOW - loops in Python)
squares = [x**2 for x in range(1000000)]

# NumPy approach (FAST - loops in C)
import numpy as np
arr = np.arange(1000000)
squares = arr**2  # Vectorized!

NumPy operations are:

  • 10x to 100x faster than pure Python
  • More readable (no loops)
  • Memory efficient

Creating Arrays

np.array([1, 2, 3])          # From list
np.zeros((3, 4))             # 3x4 matrix of 0s
np.ones(5)                   # 1D array of 1s
np.arange(0, 1, 0.1)         # Like range() but with floats
np.linspace(0, 10, 50)       # 50 evenly spaced points
Overview
main.py
Loading...
OUTPUT
Click "Run Code" to execute…