×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Reversing a List

Last Updated : May 03, 2025

In Python, reversing a list means changing the order of its elements so that the last element becomes the first, and so on. There are multiple ways to reverse a list, depending on your needs. In this chapter, we will learn how to reverse a list in Python with the help of examples.

Reverse a List Using reverse() Method

The reverse() method reverses the elements of the list in place, meaning it modifies the original list.

Example

In this example, we use the reverse() method to reverse a list of city names.

# Creating a list of cities
cities = ["Mumbai", "Delhi", "Chennai", "Kolkata"]

# Reversing the list in place
cities.reverse()

# Displaying the reversed list
print(cities)

The output of the above code would be:

['Kolkata', 'Chennai', 'Delhi', 'Mumbai']

Reverse a List Using Slicing

You can use slicing with a step of -1 to create a reversed copy of the list without modifying the original list.

Example

In this example, we reverse the list using slicing syntax.

# Creating a list of names
names = ["Amit", "Bhavna", "Chirag", "Deepa"]

# Reversing the list using slicing
reversed_names = names[::-1]

# Displaying the reversed list
print(reversed_names)

The output of the above code would be:

['Deepa', 'Chirag', 'Bhavna', 'Amit']

Reverse a List Using reversed() Function

The reversed() function returns an iterator that accesses the list in reverse order. You can convert it into a list using the list() function.

Example

In this example, we use reversed() to reverse a list of numbers.

# Creating a list of numbers
numbers = [10, 20, 30, 40, 50]

# Reversing using reversed() and converting to list
reversed_numbers = list(reversed(numbers))

# Displaying the result
print(reversed_numbers)

The output of the above code would be:

[50, 40, 30, 20, 10]

Reverse a List Using a Loop

You can also reverse a list manually using a loop by iterating from the end to the start.

Example

In this example, we reverse a list of student names using a for loop.

# Creating a list of students
students = ["Ananya", "Bharat", "Charu", "Dinesh"]

# Reversing using a loop
reversed_list = []
for i in range(len(students)-1, -1, -1):
    reversed_list.append(students[i])

# Displaying the result
print(reversed_list)

The output of the above code would be:

['Dinesh', 'Charu', 'Bharat', 'Ananya']

Reverse a List Using List Comprehension

You can reverse a list using list comprehension with the range() function.

Example

In this example, we reverse a list of fruits using list comprehension.

# Creating a list of fruits
fruits = ["Apple", "Banana", "Mango", "Guava"]

# Reversing using list comprehension
reversed_fruits = [fruits[i] for i in range(len(fruits)-1, -1, -1)]

# Displaying the result
print(reversed_fruits)

The output of the above code would be:

['Guava', 'Mango', 'Banana', 'Apple']

Exercise

Select the correct option to complete each statement about reversing lists in Python.

  1. The ___ method reverses a list in place.
  2. The ___ function returns an iterator that accesses the given list in reverse order.
  3. The slicing technique my_list[::-1] creates a ___ of the original list in reverse order.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.