When looping through a sequence, the position index and corresponding value can be retrieved at the same time using the enumerate() function.
>>>
>>> fori,vinenumerate(['tic','tac','toe']):... printi,v...0 tic1 tac2 toe
To loop over two or more sequences at the same time, the entries can be paired with the zip() function.
>>>
>>> questions=['name','quest','favorite color']>>> answers=['lancelot','the holy grail','blue']>>> forq,ainzip(questions,answers):... print'What is your {0}? It is {1}.'.format(q,a)...What is your name? It is lancelot.What is your quest? It is the holy grail.What is your favorite color? It is blue.
To loop over a sequence in reverse, first specify the sequence in a forward direction and then call the reversed() function.