Python Reverse Lists and Strings

This short post will show how to do a reverse loop in Python.

The first example will show how to do that in a list a and the other example how to reverse a string.

Also Read: Python Program to Check if a String is a Pangram or Not

Just open up a Python interpreter and test it out.

Create a list with some values in it, like this:

L1 = ["One", "two", "three", "four", "five"]

#To print the list as it is, simply do:
print L1

#To print a reverse list, do:
for i in L1[::-1]:
    print i
>>Output:
five
four
three
two
One

We can also reverse strings, like this:

string = "Hello World"

print ' '.join(reversed(string))
>>Output:

d l r o W o l l e H

Leave a Reply

Your email address will not be published. Required fields are marked *