Welcome, guest | Sign In | My Account | Store | Cart

The xzip function provides the same functionality as zip (python builtin), but utilizes a generator list comprehension so that zipped collections can be accessed iteratively.

Example: for t in xzip( xrange( 1000000 ), xrange( 1000000, 2000000, 1 ), xrange( 888,100000000, 1 ): print t

This Will begin to produce output immediately, because the collections are zipped iteratively The output of this code is exactly equivalent to:

for t in zip( xrange( 1000000 ), xrange( 1000000, 2000000, 1 ), xrange( 888,100000000, 1 ): print t

However, the second block (using zip) must first build the zipped collection entirely before the for loop can iterate over it. This could take a long time.

Note, I used xrange here so that we don't have to wait for python to build the initial lists. The xzip function would probably show its usefulness most if one had several huge collections that needed to be combined iteratively.

I developed this function to zip long lists ( >100000 ) of vertex triples with color triples in a volume visualizer.

Python, 3 lines
1
2
3
def xzip( *args ):
    return ( tuple( [ a[i] for a in args ] ) \
             for i in xrange( min( [ len(a) for a in args ] ) ) )

1 comment

Christopher Neugebauer 12 years, 8 months ago  # | flag

from itertools import izip

That is all.

Created by Tucker Beck on Mon, 30 Mar 2009 (MIT)
Python recipes (4591)
Tucker Beck's recipes (3)

Required Modules

  • (none specified)

Other Information and Tasks