• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Lists / How to reverse a list in Python

How to reverse a list in Python

Author: Aditya Raj
Last Updated: July 21, 2021

View Post

Lists in python are most used data structures. Often we need to  reverse a list, delete an element from the list, adding or inserting an element to the list or to merge two lists in python. In this article, we will look at different methods to reverse a list in python.

Reverse a list using loops

The simplest way to reverse a list is to iterate through the list in reverse order. While iterating, we can add each traversed element to a new list which will create a reversed list. For this, first we will calculate the length of the list and then we will iterate through the list in reverse order and we will append each traversed element to the new list using append() method as follows.

myList=[1,2,3,4,5,6,7]
print("Original List is:",myList)
list_len=len(myList)
newList=[]
for i in range(list_len):
    newList.append(myList[list_len-1-i])
print("reversed list is:",newList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
reversed list is: [7, 6, 5, 4, 3, 2, 1]

Although we have created a reversed list by the above method, there is an overhead of calculating the length of the list before iterating. 

We can also reverse a list using a while loop. For this In each iteration, we will pop out an element from the original list using the pop() method until the list becomes empty as follows.

myList=[1,2,3,4,5,6,7]
print("Original List is:",myList)
newList=[]
while myList: #empty list points to None and will break the loop
    temp=myList.pop()
    newList.append(temp)

print("reversed list is:",newList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
reversed list is: [7, 6, 5, 4, 3, 2, 1]

This way of reversing a list  deletes all the elements from the original list. So you should use this way to reverse a list in python only when the original list is no longer needed in the program.

Reverse a list using slicing

Slicing is an operation with the help of which we can access, update or delete elements of the list. We can also use slicing to reverse the list as follows.

myList=[1,2,3,4,5,6,7]
print("Original List is:",myList)
newList=myList[::-1]
print("reversed list is:",newList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
reversed list is: [7, 6, 5, 4, 3, 2, 1]

Using reversed() method

We can use the reversed() method to reverse the list. The reversed() method takes an iterator like list as input and returns a reverse iterator. After that, we can create the reversed list from the iterator using a for loop as follows.

myList=[1,2,3,4,5,6,7]
print("Original List is:",myList)
newList=[]
reverse_iterator=reversed(myList)
for i in reverse_iterator:
    newList.append(i)
print("reversed list is:",newList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
reversed list is: [7, 6, 5, 4, 3, 2, 1]

We can also use list comprehension instead of the for loop to create a list from reverse iterator as follows.

myList=[1,2,3,4,5,6,7]
print("Original List is:",myList)
reverse_iterator=reversed(myList)
newList= [i for i in reverse_iterator]
print("reversed list is:",newList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
reversed list is: [7, 6, 5, 4, 3, 2, 1]

We can also convert the reversed iterator into a list directly using the list() constructor as follows.

myList=[1,2,3,4,5,6,7]
print("Original List is:",myList)
reverse_iterator=reversed(myList)
newList=list(reverse_iterator)
print("reversed list is:",newList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
reversed list is: [7, 6, 5, 4, 3, 2, 1]

Reverse list using reverse() method

In all of the methods described above, we have created a new list to store the value of the original list in reversed order. Now we will see how we can reverse a list in place i.e. how we can reverse the same list which will be provided to us as input. 

To reverse a list in this way, we can use the reverse() method. The reverse() method when invoked on a list, will reverse the order of the elements as follows.

myList=[1,2,3,4,5,6,7]
print("Original List is:",myList)
myList.reverse()
print("reversed list is:",myList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
reversed list is: [7, 6, 5, 4, 3, 2, 1]

Conclusion

In this article we have seen how to reverse a list in python using a for loop, slicing, list comprehension, reverse() method and reversed() method. Stay tuned for more informative articles.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Lists Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us