Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

NumPy Reverse Vector: np.flip vs [::-1]

Reverse a NumPy vector with a[::-1] or np.flip: both return views, not copies. Covers flipud, fliplr, 2-D axes, and when to call .copy().

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

  1. Create or load the vector. a = np.arange(5) gives [0 1 2 3 4].
  2. 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.
  3. Or reverse it with np.flip. np.flip(a) gives the same [4 3 2 1 0]. Its signature is numpy.flip(m, axis=None); with the default axis=None it flips over all axes, which for a 1-D vector is the only axis.
  4. Or use np.flipud(a). The documentation describes it as reversing the order of elements along axis 0 and as equivalent to m[::-1, ...] or np.flip(m, axis=0). It requires at least a 1-D array, so it works on a vector.
  5. 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) and m[:, ::-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]) and np.shares_memory(a, np.flip(a)) both return True.
  • After r = a[::-1] and r[0] = 99, the original becomes [ 0 1 2 3 99]. Writing to the reversed result wrote to a.
  • After c = a[::-1].copy(), np.shares_memory(a, c) returns False, and changing c leaves a untouched. The ndarray.copy documentation 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.fliplr on a 1-D vector raises an error. The documentation says fliplr requires an array of at least 2-D. On NumPy 2.5.1 np.fliplr(np.arange(3)) raised ValueError: Input must be >= 2-d. For a vector, use np.flip(a), a[::-1] or np.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 was axis 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 without axis. Pass axis=0 or axis=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 with np.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.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about NumPy Reverse Vector: np.flip vs [::-1]

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →