A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.pythonforbeginners.com/code-snippets-source-code/reverse-loop-on-a-list below:

Reversing lists and strings - PythonForBeginners.com

Reversing lists and strings

Author: Last Updated: June 12, 2020

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

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.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4