This post will discuss how to reverse a list in Python.

1. Using reversed() function

The reversed() function is a built-in function that returns a reversed iterator of items from an iterable. We can use this function to reverse a list and convert the result to a list using the list() function. For example:

Download  Run Code

2. Using list.reverse() function

The reverse() function is a built-in function of the list class that reverses the order of the items in the list and modifies the original list itself. We can use this function to reverse a list in-place by calling it on the list object. For example:

Download  Run Code

3. Using extended slicing

The slicing technique is a way of accessing a subset of items from an iterable by specifying the start, stop, and step indices. We can use it to reverse a list by passing a negative step index. The slice object [::-1] will return a new list that are reversed from the original order in the list. The negative step index -1 means that the list elements are accessed from the end to the beginning.

Download  Run Code

4. Using numpy module

We can also use the numpy module to reverse a list by using the numpy.flip() function, which reverses the order of elements along an axis. For example:

Download Code

That’s all about reversing a list in Python.