Written and maintained by CASRAI Editorial Board
Last updated
To reverse a vector in NumPy, write a[::-1] or call np.flip(a). Both return a reversed view of the same data, not a new array, so the operation takes constant time and writing into the result changes the original. If you need an independent reversed array, call .copy() on the result. This guide shows each option, the one place where np.flip and [::-1] behave differently on 2-D arrays, and how to check whether you are holding a view or a copy.
What reversing a vector is used for in research
Reversing the order of a 1-D array comes up when a series stored oldest-first has to be read newest-first (or the reverse), when a sorted array has to be read from largest to smallest, and when a lookup table or kernel has to be mirrored. For the sorting case, np.argsort returns indices in ascending order, and slicing those indices with [::-1] gives descending order; see the CASRAI guide to np.argsort for the full workflow. Reversing is a different operation from transposing: a transpose swaps axes, while a reversal keeps the axes and changes the order of the entries along them (see np.transpose).
Install or access
NumPy is free and open source. The official installation page, numpy.org/install, lists several routes, including uv, pip and conda. With pip:
pip install numpy
The page recommends doing this inside a virtual environment (python -m venv my-env, then activate it and run the pip command). With conda, the page gives:
conda create -n my-env
conda activate my-env
conda install numpy
and notes that NumPy can be installed from the defaults or conda-forge channels (the page also lists uv pip install numpy as a recommended option). All examples below begin with import numpy as np.
How to reverse a vector in NumPy: step by step
- Create or load the vector.
a = np.arange(5)gives[0 1 2 3 4]. - Reverse it with slicing.
a[::-1]gives[4 3 2 1 0]. The negative step makes stepping go towards smaller indices; when the start and stop are left out, a negative step defaults them to the last element and to the beginning of the array, so the whole vector is returned in reverse. - Or reverse it with
np.flip.np.flip(a)gives the same[4 3 2 1 0]. Its signature isnumpy.flip(m, axis=None); with the defaultaxis=Noneit flips over all axes, which for a 1-D vector is the only axis. - Or use
np.flipud(a). The documentation describes it as reversing the order of elements along axis 0 and as equivalent tom[::-1, ...]ornp.flip(m, axis=0). It requires at least a 1-D array, so it works on a vector. - Make a copy only if you need one.
b = a[::-1].copy()gives an independent reversed array (see the next section).
np.flip vs [::-1] vs flipud: which should you use?
For a 1-D vector all three return the same values, and the NumPy documentation states the equivalences directly. Pick the one that reads best in your code: np.flip(a) says what it does, and a[::-1] is shorter. The difference appears with 2-D arrays and above.
| Goal | Expression | Equivalent forms stated in the NumPy docs |
|---|---|---|
| Reverse a 1-D vector | np.flip(a) |
a[::-1], np.flipud(a) |
| Reverse the rows of a 2-D array (axis 0) | np.flip(m, 0) |
np.flipud(m), m[::-1, ...] |
| Reverse the columns of a 2-D array (axis 1) | np.flip(m, 1) |
np.fliplr(m), m[:, ::-1] |
| Reverse every axis | np.flip(m) |
m[::-1, ::-1, ..., ::-1] |
The warning that matters: on a 2-D array, np.flip(m) with the default axis=None reverses every axis, while m[::-1] reverses only the first. Worked example, run on NumPy 2.5.1 with m = np.arange(6).reshape(2, 3):
m[::-1]returns[[3 4 5], [0 1 2]]: the rows swap places, each row keeps its order.np.flip(m)returns[[5 4 3], [2 1 0]]: rows and columns are both reversed.np.flip(m, 1)andm[:, ::-1]both return[[2 1 0], [5 4 3]]: only the columns are reversed.
Axis arguments can be negative: they count from the last axis to the first, and a tuple of axes flips all axes listed.
Is a reversed array a copy or a view?
A view. The numpy.flip documentation says it returns “a view of m with the entries of axis reversed” and that, since a view is returned, the operation is done in constant time. The indexing documentation adds that all arrays generated by basic slicing are always views of the original array, which covers a[::-1]. Advanced indexing, by contrast, always returns a copy.
Observed on NumPy 2.5.1 with a = np.arange(5):
np.shares_memory(a, a[::-1])andnp.shares_memory(a, np.flip(a))both returnTrue.- After
r = a[::-1]andr[0] = 99, the original becomes[ 0 1 2 3 99]. Writing to the reversed result wrote toa. - After
c = a[::-1].copy(),np.shares_memory(a, c)returnsFalse, and changingcleavesauntouched. Thendarray.copydocumentation describes the result as a copy of the array that is independent of the original.
Practical consequence: if the reversed vector will be modified, or the original must stay unchanged, add .copy(). If you only read from it, the view is cheaper.
Output and how to read it
The result is an ndarray with the same shape and dtype as the input, with entries in the opposite order along the chosen axis. Check it by comparing endpoints: r[0] == a[-1] and r[-1] == a[0]. To confirm that two forms agree, compare them with np.all(np.flip(A, 2) == A[:, :, ::-1, ...]), the same check the NumPy documentation uses; it returns True. The .base attribute and np.shares_memory tell you whether you hold a view.
Troubleshooting
np.fliplron a 1-D vector raises an error. The documentation saysfliplrrequires an array of at least 2-D. On NumPy 2.5.1np.fliplr(np.arange(3))raisedValueError: Input must be >= 2-d.For a vector, usenp.flip(a),a[::-1]ornp.flipud(a).np.flip(a, axis=1)on a 1-D vector raises an AxisError. A vector has only axis 0 (or -1). On NumPy 2.5.1 the message wasaxis 1 is out of bounds for array of dimension 1.- Both axes flipped when you wanted one. You called
np.flip(m)on a 2-D array withoutaxis. Passaxis=0oraxis=1, or use slicing. - The original changed after you edited the reversed array. The result is a view. Use
.copy(). - Python lists.
[::-1]on a NumPy array is slicing and returns a view; the same syntax on a Python list builds a new list. Convert withnp.asarray(lst)first if you want the NumPy behaviour. - Descending order after sorting. Reversing a sorted vector gives descending order, and for ordering by another variable use the index approach in the np.argsort guide.
Free alternatives
Every option below ships with NumPy itself, so no extra package is required. Each row reflects the NumPy reference pages listed under Sources.
| Function or syntax | What the documentation says |
|---|---|
np.flip(m, axis=None) |
Returns a view with the entries of the given axis reversed; default flips all axes; accepts an int or tuple of ints. |
np.flipud(m) |
Reverses the order of elements along axis 0; equivalent to m[::-1, ...]; requires at least a 1-D array. |
np.fliplr(m) |
Reverses the order of elements along axis 1; equivalent to m[:, ::-1]; requires at least a 2-D array. |
| Slicing with a negative step | Basic slicing always returns a view of the original array. |
ndarray.copy() |
Returns an independent copy, used after a reversal when you need separate memory. |
FAQ
How do I reverse a vector in NumPy?
Use a[::-1] or np.flip(a). Both return the entries in opposite order and both are views.
Does a[::-1] copy the array?
No. The NumPy indexing documentation states that arrays generated by basic slicing are always views. Call .copy() if you need independent data.
What is the difference between np.flip and np.flipud?
For a vector, nothing. For 2-D arrays, np.flipud(m) equals np.flip(m, axis=0), whereas np.flip(m) with no axis reverses every axis.
Can I reverse a 2-D array’s columns only?
Yes: np.flip(m, 1), np.fliplr(m) or m[:, ::-1].
Is reversing the same as transposing?
No. Transposing swaps axes; reversing changes the order of entries along an axis. See the np.transpose guide, and for a related element-wise function see np.abs.
Sources
Links checked 2026-10-29.
- NumPy documentation, numpy.flip, checked 2026-10-29.
- NumPy documentation, numpy.flipud, checked 2026-10-29.
- NumPy documentation, numpy.fliplr, checked 2026-10-29.
- NumPy documentation, Indexing on ndarrays, checked 2026-10-29.
- NumPy documentation, numpy.ndarray.copy, checked 2026-10-29.
- NumPy, Installing NumPy, checked 2026-10-29.







