Saturday, September 12, 2009

Iterating through multiple objects at once in python

As part of my project coding some image processing using OpenCV/Python I needed to iterate through two images at once (the images have identical dimensions). Please note that for this i'm using the OpenCV 1.1pre1 build and Python 2.6. After a quick Google search i found this page which details the various methods for iterating through multiple objects at once in python.

I used the "zipping" method; this basically takes both iteratable objects and joins one sequential item from each iterable object together to form a tuple. This tuple is then added to a list which contains all the object pairs. The zip will stop at the end of the shortest iterable object (so the final list will be as long as the shortest object).