Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » Python programs

Python | Iterate a list in reverse order



Here, we are going to learn how to iterate a list in reverse order in Python? To iterate list in reverse order we use list[::-1] notation.
Submitted by IncludeHelp, on August 04, 2018

Given a list and we have to iterate it in reverse order in python.

Example:

    Input:
    List = [10, 20, 30, 40, 50]
    Output:
    list = [50, 40, 30, 20, 10]


    Input;
    list = ['Hello', 10 'World', 20]
    Output:
    list = [20, 'World', 10, 'Hello']

Iterate a list in reverse order

To iterate a list in reverse order, list[::-1] is used. list[::-1] will return the list in reverse order.

Program:

# define a list
list1  = [10, 20, 30, 40, 50]

# print the list 
print "original list: ", list1

# iterate the list
list1 = list1[::-1]

# print the list 
print "list in reverse order: ", list1

# another list with string and integer elements
list2 = ['Hello', 10, 'world', 20]

# print the list
print "Original list: ", list2

# iterate the list
list2 = list2[::-1]

# print the list
print "list in reverse order: ", list2

Output

    original list:  [10, 20, 30, 40, 50]
    list in reverse order:  [50, 40, 30, 20, 10]
    Original list:  ['Hello', 10, 'world', 20]
    list in reverse order:  [20, 'world', 10, 'Hello']





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.