How to use Python's enumerate and zip to iterate over two lists and their indices.
enumerate - Iterate over indices and items of a list¶
The Python Cookbook (Recipe 4.4) describes how to iterate over items and indices
in a list using enumerate. For example:
alist = ['a1', 'a2', 'a3']
for i, a in enumerate(alist):
print i, a
Results:
0 a1 1 a2 2 a3
zip - Iterate over two lists in parallel¶
I
previously wrote about using zip to iterate over two lists
in parallel. Example:
alist = ['a1', 'a2', 'a3']
blist = ['b1', 'b2', 'b3']
for a, b in zip(alist, blist):
print a, b
Results:
a1 b1 a2 b2 a3 b3
enumerate with zip¶
Here is how to iterate over two lists and their indices using enumerate together with zip:
alist = ['a1', 'a2', 'a3']
blist = ['b1', 'b2', 'b3']
for i, (a, b) in enumerate(zip(alist, blist)):
print i, a, b
Results:
0 a1 b1 1 a2 b2 2 a3 b3
Related posts
- How to sort a list of dicts in Python — posted 2010-04-02
- Python setdefault example — posted 2010-02-09
- How to conditionally replace items in a list — posted 2008-08-22
- How to invert a dict in Python — posted 2008-01-14
- How to find the intersection and union of two lists in Python — posted 2008-01-03
7
Comments
—
Comments feed for this post
#2 Jeremy Lewis commented on 2009-05-29:
>>> def foo():
... for i, x, y in izip(count(), a, b):
... pass
...
>>> def bar():
... for i, (x, y) in enumerate(zip(a, b)):
... pass
...
>>> delta(foo)
0.0213768482208
>>> delta(bar)
0.180979013443
where a = b = xrange(100000) and delta(f(x)) denotes the runtime in seconds of f(x).
#3 Eliot commented on 2009-05-29:
Jeremy,
Thanks for the tip and the clear example and demonstration of the performance benefit. I had heard of itertools but have not really used it. It was great to talk to you today and I hope I can talk to you again soon.
#6 Nitin commented on 2011-11-01:
I have set of n set, each with different number of elements I wanted to find all possible combinations of n elements, one from each set. Consider two sets (e1,e2,e3) (e4,e5)
output required is as follows
(e1,e4)
(e1,e5)
(e2,e4)
(e2,e5)
(e3,e4)
(e3,e5)
I do not know the number of such sets in advance.
#7 Eliot commented on 2011-11-01:
Nitin: http://www.saltycrane.com/blog/2011/11/find-all-combinations-set-lists-itertoolsproduct/
Post a comment
About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(5)
-
aws
(9)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(13)
-
conkeror
(2)
-
core
(2)
-
cygwin
(17)
-
datastructures
(14)
-
datetime
(4)
-
decorators
(4)
-
django
(40)
-
emacs
(22)
-
files_directories
(11)
-
git
(5)
-
hardware
(5)
-
install_setup
(8)
-
javascript
(3)
-
keyboard
(9)
-
matplotlib
(5)
-
mercurial
(4)
-
nginx
(2)
-
persistence
(5)
-
preferences
(7)
-
processes
(4)
-
pyqt
(18)
-
python
(144)
-
ratpoison
(3)
-
regexes
(6)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(14)
-
ssh
(10)
-
subversion
(6)
-
twisted
(7)
-
ubuntu
(65)
-
urxvt
(5)
-
vxworks
(25)
-
webdev
(5)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Marty Alchin
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan
#1 Jeremy Lewis commented on 2009-05-29:
If you're working with last lists and/or memory is a concern, using the itertools module is an even better option.
yields the exact same result as above, but is faster and uses less memory.