If you want to iterate through a list in reverse order, you can do so by stepping over (iterating through) the indexes of the list in reverse order. So in Python, for example, you would use
range (or
xrange for a potentially long list) with an increment of -1, giving the final index number as the start point and the beginning as the end:
have = [20,40,60]
for p in range(len(have)-1,-1,-1):
print p, have[p]
Remember that Python's range starts at the first value given, but stops short of the terminating value. So you need to stop at "-1" to ensure that you process element number 0
If you're happy to have the list altered, many languages include a reverse function. Again in Python:
have.reverse()
for val in have:
print val
Or you could use a "pop" fucntion to keep removing and returning the final item until the list is empty. Let's see that in Perl for a change:
@numbers = (4,8,7,34);
while ($val = pop @numbers) {
print "$val\n";
}
Full Python example is
[here]. These techniques also apply to other languages with "ordered collections" - whether they're known as lists, arrays, tuples, tables, or vectors.
(written 2012-03-23, updated 2012-03-24)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y104 - Python - Lists and Tuples [383] Overloading of operators on standard objects in Python - (2005-07-19)
[657] The ternary operator in Python - (2006-03-25)
[899] Python - extend v append on a list - (2006-10-20)
[955] Python collections - mutable and imutable - (2006-11-29)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1641] Tektronix 4010 series / Python Tuples - (2008-05-13)
[1789] Looking for a value in a list - Python - (2008-09-08)
[2280] Creating and iterating through Python lists - (2009-07-12)
[2284] Strings as collections in Python - (2009-07-12)
[2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[2719] Traffic lights in Python - (2010-04-13)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[3181] Beware - a=a+b and a+=b are different - Python - (2011-02-23)
[3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
[3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
[3763] Spike solutions and refactoring - a Python example - (2012-06-13)
[4027] Collections in Python - list tuple dict and string. - (2013-03-04)
[4368] Shuffling a list - Ruby and Python - (2014-12-28)
[4722] Embedding more complex code into a named block - (2016-11-04)
P208 - Perl - Lists [28] Perl for breakfast - (2004-08-25)
[140] Comparison Chart for Perl programmers - list functions - (2004-12-04)
[230] Course sizes - beware of marketing statistics - (2005-02-27)
[240] Conventional restraints removed - (2005-03-09)
[355] Context in Perl - (2005-06-22)
[463] Splitting the difference - (2005-10-13)
[560] The fencepost problem - (2006-01-10)
[622] Queues and barrel rolls in Perl - (2006-02-24)
[762] Huge data files - what happened earlier? - (2006-06-15)
[773] Breaking bread - (2006-06-22)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[968] Perl - a list or a hash? - (2006-12-06)
[1304] Last elements in a Perl or Python list - (2007-08-16)
[1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
[1703] Perl ... adding to a list - end, middle, start - (2008-07-09)
[1828] Perl - map to process every member of a list (array) - (2008-10-09)
[1917] Out of memory during array extend - Perl - (2008-12-02)
[1918] Perl Socket Programming Examples - (2008-12-02)
[2067] Perl - lists do so much more than arrays - (2009-03-05)
[2226] Revision / Summary of lists - Perl - (2009-06-10)
[2295] The dog is not in trouble - (2009-07-17)
[2484] Finding text and what surrounds it - contextual grep - (2009-10-30)
[2813] Iterating over a Perl list and changing all items - (2010-06-15)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3870] Writing more maintainable Perl - naming fields from your data records - (2012-09-25)
[3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
[3939] Lots of ways of doing the same thing in Perl - list iteration - (2012-12-03)
[4609] Mapping an array / list without a loop - how to do it in Perl 6 - (2016-01-03)
Some other Articles
Object oriented or structured - a comparison in Python. Also writing clean regular expressionsMelksham Train Service - please support the TransWilts case for regular trainsFuture train services in Wiltshire - please write in to support a better serviceReading Google Analytics results, based on the relative populations of countriesStepping through a list (or an array) in reverse orderKings Cross - new concourse - between Python in Cambridge and Objective C in LondonA modern area of Cambridge - some thoughts provoked?Makefile variables - defined internally, from the command line and from the environmentWill will smile?Error checking in a Python program - making your program robust via exceptions