Written and maintained by CASRAI Editorial Board
Last updated
numpy.ravel(a, order='C') returns a contiguous, flattened one-dimensional array holding the elements of a, and it makes a copy only if it needs to. That last clause is the whole reason to prefer it over ndarray.flatten() on large data: flatten always copies, ravel does not have to. The other thing to settle before you call it is order, because it decides which element comes first when your array has more than one axis.
This guide follows the NumPy v2.5 manual (numpy.ravel and ndarray.flatten pages, read 2026-11-07). The outputs shown were run on NumPy 2.5.1. It covers the four order values side by side, a research-data example, the difference between ravel, flatten and reshape(-1), and the cases where the result is not the plain 1-D ndarray you expected.
What ravel is used for in research code
Many analysis routines expect a single column of values: a histogram, a quantile, a one-sample test, a plotting call. Measurements usually arrive as a 2-D or 3-D array (subjects by timepoints, samples by replicates, image rows by columns). np.ravel turns that array into one long vector without writing a loop. If you only need to pool every value, the order does not matter. If the position of each value matters afterwards, you must choose the order deliberately, as the table below shows.
Install or access
NumPy’s install page (numpy.org/install) lists pip install numpy and, for conda, creating and activating an environment (conda create -n my-env, then conda activate my-env) followed by conda install numpy. Then check the install:
import numpy as np
print(np.__version__)
New to the import line? See import numpy as np.
Step by step: flattening an array with np.ravel
Syntax and parameters
- Import NumPy:
import numpy as np. - Build or load your array:
x = np.array([[1, 2, 3], [4, 5, 6]]). - Call the function:
np.ravel(x). The method formx.ravel()does the same thing. - Choose
orderonly if you need something other than the default'C':np.ravel(x, order='F').
The signature has two parameters: a, the input (array_like), and order, one of 'C', 'F', 'A', 'K', default 'C'. It returns y, a contiguous 1-D array of the same type as the input, with shape (a.size,). Any other string for order raises ValueError: order must be one of 'C', 'F', 'A', or 'K'.
The four order values
The manual says 'C' and 'F' ignore the memory layout of the input and refer only to axis indexing, while 'A' and 'K' depend on how the data sits in memory.
| order | What the manual says | Result for x = [[1,2,3],[4,5,6]] |
|---|---|---|
'C' (default) |
Index the elements in row-major order, with the last axis index changing fastest. | [1 2 3 4 5 6] |
'F' |
Index the elements in column-major order, with the first index changing fastest. | [1 4 2 5 3 6] |
'A' |
Fortran-like order if the array is Fortran contiguous in memory, C-like order otherwise. | For x.T: [1 2 3 4 5 6] |
'K' |
Elements in the order they occur in memory, except reversing the data when strides are negative. | For x.T: [1 2 3 4 5 6] |
The transpose makes the difference visible. With the same x, np.ravel(x.T) gives [1 4 2 5 3 6], because the default 'C' follows the transposed axes, while np.ravel(x.T, order='A') gives [1 2 3 4 5 6], because the transpose is a Fortran-contiguous view of the original memory. The manual’s three-axis example makes the same point: for a = np.arange(12).reshape(2,3,2).swapaxes(1,2), a.ravel(order='C') is [0 2 4 1 3 5 6 8 10 7 9 11] and a.ravel(order='K') is [0 1 2 3 4 5 6 7 8 9 10 11]. For the transpose operation itself, see NumPy transpose.
A research-data example: subjects by timepoints
Suppose d holds two subjects measured at three timepoints, one row per subject:
d = np.array([[10, 11, 12],
[20, 21, 22]])
np.ravel(d) # array([10, 11, 12, 20, 21, 22])
np.ravel(d, order='F') # array([10, 20, 11, 21, 12, 22])
With 'C' the vector lists each subject’s timepoints together (subject 1 at t1, t2, t3, then subject 2). With 'F' it lists every subject at the first timepoint, then every subject at the second, and so on. If you plan to rebuild the 2-D layout later with reshape, use the same order in both calls: reshape also accepts order, and the NumPy manual states that ravel is equivalent to reshape(-1, order=order). This example was computed with the arrays above; it is a worked illustration, not data from a study.
Output and how to read it
- Shape. Always
(a.size,). A (2, 3) array gives shape (6,). A scalar such asnp.ravel(5)gives shape (1,). - Type. Since NumPy 1.10 the returned array has the same type as the input subclass. A masked array in gives a masked array out; a
np.matrixin gives a 1-Dndarrayout. - Copy or view. The manual says a copy is made only if needed. It does not promise a view, so do not rely on writing into the result to change the original. In a run on NumPy 2.5.1,
x.ravel()on a C-contiguous array shared memory withx(assigningr[0] = 99changedx), whilenp.shares_memory(x, x.flatten())wasFalse. Treat that as an observation, not a guarantee, and call.copy()yourself when you need an independent array.
ravel vs flatten vs reshape(-1)
| Call | Copy behaviour (per the NumPy manual) | order values accepted |
|---|---|---|
np.ravel(a) / a.ravel() |
“A copy is made only if needed.” | C, F, A, K |
a.flatten() |
Returns “A copy of the input array, flattened to one dimension.” | C, F, A, K |
a.reshape(-1) |
The ravel page advises it “when a view is desired in as many cases as possible.” | C, F, A (the ravel page notes reshape does not support K) |
Practical reading of that table: use flatten() when you want a guaranteed independent copy, ravel() when you want to avoid copying where possible and need 'K', and reshape(-1) when you want a view in as many cases as possible. The ndarray.flatten manual also lists 'F': np.array([[1,2],[3,4]]).flatten('F') is [1 3 2 4].
Troubleshooting
“ravel returned a 2-D array”
This happens with np.matrix and its method form. In a run on NumPy 2.5.1, np.ravel(m) for m = np.matrix([[1, 2], [3, 4]]) returned a plain 1-D ndarray of shape (4,), as the manual describes, but m.ravel() and m.flatten() returned np.matrix objects of shape (1, 4), because matrix is always two-dimensional. Use the function form np.ravel(m), or convert first with np.asarray(m).
Masked values come back as a masked array
np.ravel on a masked array returns a masked array with the mask flattened too (for np.ma.array([[1, 2], [3, 4]], mask=[[0, 1], [0, 0]]) the result has data [1 -- 3 4]). If a downstream function needs plain numbers, deal with the mask explicitly, for example by filling or compressing it, before analysis.
The values are in the wrong order
Check order. A transposed or sliced array can give a different sequence under 'C' and 'K', as the transposed example above shows. If a later step depends on the sequence, state the order explicitly instead of relying on the default.
Changing the result changed my original
The result can share memory with the input when no copy was needed. Use a.flatten() or np.ravel(a).copy() when you are going to modify it.
ValueError about order
Only 'C', 'F', 'A' and 'K' are accepted. Lowercase or other strings are not covered by the manual; use the four documented values.
Free alternatives and neighbouring functions
| Tool | What it does for flattening | Source |
|---|---|---|
ndarray.flatten() |
Always returns a flattened copy. | NumPy manual, ndarray.flatten |
ndarray.reshape(-1) |
Same shape result as ravel in C order; the ravel page prefers it when a view is wanted in as many cases as possible. | NumPy manual, numpy.ravel |
numpy.ma.ravel, numpy.record.ravel |
Variants for masked arrays and record arrays; they appear as separate pages in the manual. | NumPy manual |
For adjacent array operations in the same library, see broadcasting in NumPy, numpy.vstack for stacking arrays, NumPy delete for removing rows and columns, and NumPy argmax, which returns an index into the flattened array when no axis is given.
Frequently asked questions
What does ravel mean in NumPy?
In NumPy, ravel means to flatten a multi-dimensional array into a one-dimensional array, returning a contiguous result and copying the data only when needed. Source: numpy.ravel, NumPy v2.5 manual.
What is the difference between numpy ravel and flatten?
flatten() always returns a copy. ravel() returns a copy only if needed, so it can be cheaper on large arrays. Both accept the same four order values.
How do I flatten an array in NumPy?
Call np.ravel(a), a.ravel(), a.flatten() or a.reshape(-1). They return the same sequence for the default C order; they differ in whether the result may share memory with the original.
Does ravel change the original array?
No, the call does not alter the input. But the returned array can share memory with it when no copy was made, so assigning into the result can change the original.
Which order should I use?
Use 'C' (the default) for row-by-row sequences and 'F' for column-by-column. Use 'K' only when you want the memory sequence and do not care about axis order.
Sources
- NumPy v2.5 manual, numpy.ravel, read 2026-11-07.
- NumPy v2.5 manual, numpy.ndarray.flatten, read 2026-11-07.
- NumPy, Installing NumPy, read 2026-11-07.
- Example outputs and the memory-sharing, matrix and masked-array observations were run by CASRAI on NumPy 2.5.1 on 2026-11-07.







