Reverse list in Python Tutorial + Reverse Loops + Source Code

Posted: September 12, 2014 in Programming, Python
Tags: , , , , , , , , ,

This is a quick tutorial on reverse loops in Python 2.7.x  and also will be showing you an example on how to reverse a list. There are many methods you can do achieve this. In this tutorial, we will not be using the list.reverse() method which is simpler but will be using a simple loop and another method i.e. using the reversed() function to learn more about reverse loops.Hope this helps!

Lets say we have some numbers stored in a list (u can make use of any random numbers, here I have used numbers 0-9 for simplicity) as follows:

number_list= [0,1,2,3,4,5,6,7,8,9]

And now if we need to store & display these numbers in reverse order, we can use the reverse loop as follows:

## Reverse Loops in Python 2.7.x
number_list= [0,1,2,3,4,5,6,7,8,9]       ## Sample list 

print "Original List = ", number_list  ## Displays list

reversed_list=[]        ## Create an empty list named reversed_list

for item in number_list[::-1]:      ## Reverse Loop
    reversed_list.append(item)      ## Add/Append current item to reversed_list

print "Reversed List = ", reversed_list ## Print Reversed List

Output:

Original List =  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Reversed List =  [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Alternative Method  (using reversed function):

Consider the below list:

number_list= [0,1,2,3,4,5,6,7,8,9]

First, we will calculate the size/length of the list, by using the function len() and storing it into a variable size_of_list

size_of_list=len(number_list)

Here, the value of  the size_of_list will be 10.

[NOTE :Here range(10) will be a list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] . And we use reversed(range(10)) function to reverse the values of range list, and then use the for loop to assign the first value of item as 9, second value as 8 and so on. ]

## Reverse Loop in Python 2.7.x using reversed function

number_list= [0,1,2,3,4,5,6,7,8,9]       ## Sample list
print "Original List = ", number_list  ## Displays list

reversed_list=[]            ## Create an empty list named reversed_list
size_of_list=len(number_list) ## Store Size/length of list , (here length is 10)

for item in reversed(range(size_of_list)): ## Reverse loop
    reversed_list.append(number_list[item]) ## Add/Append current item to reversed_list

print "Reversed List = " , reversed_list  ## Print Reversed List

Output:

Orginal List =  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Reversed List =  [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Feel feel to leave a comment if you have any queries or want to reach out to me. You can also Follow/Subscribe to my blog to get future blog posts! Happy Bloggin!🙂

Regards
ΞXΤЯΞМΞ

Comments
  1. Vishnu Bhaskaran says:

    Will come in handy ! Thanks for the same !

  2. Sarath Sp says:

    Hey for reversing a list all one need to do is

    let “K” be the list

    reversedK = K[::-1]

    • ΞXΤЯ3МΞ says:

      @Sarath, That should work too. I created this guide with the intention that beginners will understand working of loops too in the process. But thanks a ton for sharing!🙂

      Regards,
      ΞXΤЯ3МΞ

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s