Iterate over a sequence in reverse order in python
By: Python Documentation Team Printer Friendly Format
Use the reversed() built-in function, which is new in Python 2.4:
for x in reversed(sequence):
... # do something with x...
This won’t touch your original sequence, but build a new copy with reversed order to iterate over.
With Python 2.3, you can use an extended slice syntax:
for x in sequence[::-1]:
... # do something with x...
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
How to install Jupyter in Ubuntu and make it accessible through Apache Reverse Proxy
Python Basics - Setting up your Python Development Environment
Schwartzian Transform in python
Multidimensional list (array) in python
Remove duplicates from a list in python
Convert number to string in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Archived Comments
1. Brettkip
View Tutorial By: Brettkip at 2017-06-05 08:26:17