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
- An example using Python's groupby and defaultdict to do the same task — posted 2014-10-09
- python enum types — posted 2012-10-10
- Python data object motivated by a desire for a mutable namedtuple with default values — posted 2012-08-03
- 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
Comments
If you're working with last lists and/or memory is a concern, using the itertools module is an even better option.
from itertools import izip, count
alist = ['a1', 'a2', 'a3']
blist = ['b1', 'b2', 'b3']
for i, a, b in izip(count(), alist, blist):
print i, a, b
yields the exact same result as above, but is faster and uses less memory.
>>> 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).
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.
Thanks for the zip example, I grok it now.
Jeremy, Thanks for the example, It is very helpful.
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.
Nitin: http://www.saltycrane.com/blog/2011/11/find-all-combinations-set-lists-itertoolsproduct/
In order to use zip to iterate over two lists - Do the two lists have to be the same size? What happens if the sizes are unequal? Thanks.
Thx man helped me alot nice example btw
re:#8, unequal list length: the result is truncated to the shorter list. See below for a discussion of how to use the longest list instead: http://stackoverflow.com/questions/1277278/python-zip-like-function-that-pads-to-longest-length
short answer for py2.6+: use "map(None, alist, blist)"
dunno what the equivalent is in py3+
when iterating through unequal length lists using itertools
import itertools
a1=[1,2,3,6,7,9]
c1=['a','a','c','d']
b1=[10,20,30,40,50,60]
d1=[11,12,13,14,15,16,17,18]
mylist = list(itertools.izip_longest(a1,b1,c1,d1))
a1=[1,2,3,6,7,9]
for items in mylist:
litems=list(items)
if items[0] is not None and items[1] is not None:
a_old = items[0]
b_old = items[1]
if items[0] is None and items[1] is None:
litems[0]= a_old
litems[1]= b_old
a,b,c,d=litems
print a,b,c,d
is ther any other better way to give previous value if None occurs for any field.
disqus:2412310580
Very useful page with clear examples, thanks.
disqus:3273150118