enumerate and zip are built-in Python functions that can be used on sequences.
# Python enumerate and zip Built-in Functions `enumerate()` and `zip()` are built-in functions that can be used on sequences. ## enumerate() The `enumerate()` function can be use to iterate over indices and items of a list. Here's an example using a for loop to print the index (i) and value (v). ```python >>> a = ["a", "b", "c"] >>> for i, v in enumerate(a): print i, v 0 a 1 b 2 c ``` ## zip() The `zip()` function can be used to iterate over two or more lists in parallel. ```python >>> a = [1, 2, 3] >>> b = [3, 4, 5] >>> c = [6, 7, 8] >>> for i, j, k in zip(a, b, c): print i, j, k 1 3 6 2 4 7 3 5 8 ``` ### Use zip() and enumerate() together Here is how to iterate over two lists and their indices using `enumerate()` together with `zip()`. ```python >>> alist = ['a1', 'a2', 'a3'] >>> blist = ['b1', 'b2', 'b3'] >>> >>> for i, (a, b) in enumerate(zip(alist, blist)): print i, a, b 0 a1 b1 1 a2 b2 2 a3 b3 ``` --- **References** - [How to use Python's enumerate and zip to iterate over two lists and their indices](http://www.saltycrane.com/blog/2008/04/how-to-use-pythons-enumerate-and-zip-to/) - [Python enumerate() and zip() built in functions](http://editdistance.com/python-enumerate-and-zip-built-in-functions/)