A python for-loop can take any sequence as an argument, but sometimes you want both an index and the elements of a sequence. One way is to loop over the indices:
for i in xrange(len(sequence)):
e = sequence[i]
# do something with both i and e
The idiom I hadn't come up with on my own was:
for i, e in zip(range(len(sequence)),sequence
(
Read more... )