Reversing lists and strings - PythonForBeginners.com

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

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

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

Related Posts:

List Comprehensions in Python

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.