If I\'m making a simple grid based game, for example, I might have a few 2d lists. One might be for terrain, another might be for objects, etc. Unfortunately, when I need to ite
Generator expressions and izip
from itertools module will do very nicely here:
from itertools import izip
for a, b in (pair for (aline, bline) in izip(alist, blist)
for pair in izip(aline, bline)):
if a.isWhatever:
b.doSomething()
The line in for
statement above means:
alist
and blist
and make a tuple from them (aline, bline)
izip
again and take each element from them (pair
).This method has two advantages:
zip
and use more efficient generators with izip
instead.