Thursday, April 17, 2014

Traversing a list backwards in Python

I would never have figured this out. Apparently there is a third parameter to list indexing which is step size. Setting that to -1 causes a for loop to operate on each list element from the last to the first.

for item in my_list[::-1]:
print item

http://stackoverflow.com/questions/529424/traverse-a-list-in-reverse-order-in-python