>>> for i, c in enumerate('test'): ... print i, c ... 0 t 1 e 2 s 3 t
Here is what the above code is Doing:
1. It’s creating a list of tuples, where each tuple is a pair of an index and a character.
2. It’s iterating over that list of tuples.
3. It’s unpacking each tuple into two variables, i and c.
4. It’s printing those two variables.
You can use this same pattern to iterate over two lists at the same time. For example:
>>> list1 = [3, 6, 9]
>>> list2 = [4, 8, 12]
>>> for i, j in zip(list1, list2):
… print i + j
…
7
14
21