Python lists are flexible containers; NumPy arrays trade flexibility for speed and memory efficiency on numeric data.
Side-by-side comparison
| Feature | Python list | NumPy ndarray |
|---|---|---|
| Element types | Mixed | Single dtype |
| Math | Loops or list comprehensions | Vectorized ufuncs |
| Memory | Pointer per element | Contiguous buffer |
| Shape | 1D only (nested lists for 2D) | Native n-dimensional |
When to use each
- Lists — ragged data, mixed types, small collections, general Python
- NumPy — numeric computation, large homogeneous data, linear algebra
Same operation, different style
# Python list
lst = [1, 2, 3]
doubled = [x * 2 for x in lst]
# NumPy
import numpy as np
arr = np.array([1, 2, 3])
doubled = arr * 2
Important interview questions and answers
- Q: Can you append to ndarray like list.append?
A: Not efficiently—ndarrays have fixed size; use np.concatenate or preallocate. - Q: List + list?
A: [1,2] + [3,4] concatenates; np.array([1,2]) + np.array([3,4]) adds element-wise.
Self-check
- Name two advantages of ndarray over list for math.
- When should you keep using Python lists?
Pitfall: Using + on lists vs arrays—different semantics.
Interview prep
- Speed?
NumPy uses contiguous typed buffers and C loops—much faster on large numeric data.
- When lists?
Mixed types, ragged structures, or small collections.