ravel() and flatten() (NumPy)
Both ravel() and flatten() collapse a multi-dimensional array into a 1-D array. The key difference is that ravel() returns a view of the original data when possible, while flatten() always returns an independent copy.
NumPy
When you need a flat sequence of every element — to feed a function, plot, or loop — ravel and flatten both deliver one. They read the elements in row-major (C) order by default, left to right and top to bottom.
The practical difference is memory and side effects. ravel() avoids copying when it can, so modifying its result may alter the original array; flatten() copies, so its result is safe to modify independently but uses more memory. Choose ravel for speed when you won't mutate, flatten when you need a detached copy.
Both are the inverse intent of reshape into higher dimensions, and ravel is essentially reshape(-1) that prefers a view.
Syntax
a.ravel() | a.flatten()Examples
Python
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a.ravel())Output:
[1 2 3 4]