Python reverse list | with reverse() function & Other ways

You can reverse Python List using an inbuilt reverse() function and other ways. Python reverse() function is mainly used to reverses the elements of a given list(Array).

In this tutorial our main top will be reverse() function.

Syntax

doesn’t take any argument.

Return Value

It Doesn’t return any value. List elements will updated in reversed order.

Way to reverse a list in Python

  • Python reverse() function
  • Using the reversed() function
  • Using the slicing technique.
  • Loops: for-loop and while loop

Python reverse list Examples

1. reverse() function

It will modify the original list.

Output: [7, 6, 3, 4, 1]

2. reversed() function

we get a reverse iterator which we use to cycle through the list.

Output: [5, 4, 3, 2, 1, 0]

3. Reverse a List Using Slicing Operator

4. Using for loop & reversed() function

Output: Linux
macOS
Windows

Q: How to reverse array in Python?

Answer: You can use reverse() function to reverse a Array elements in python. Array and list are same thing python programming.

Q: How to reverse a list in python using for loop?

Answer: You can use for-loop which reverses a list in python without reverse function. See the below example program.

Output: [5, 4, 3, 2, 1]

Reverse empty list

No error occur and list will be same because no elements in the list.

Output: []

Do comment if you knew any other method, doubts, or suggestion in the comment section on this tutorial.

Note:
IDE: PyCharm 2020.1.1 (Community Edition)
macOS 10.15.4
Python 3.7
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.