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.
Just open up a Python interpreter and test it out.
- Python Lists Cheat Sheet
- Python Programming – List Functions And Methods
- String Manipulation in Python
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