inverse of izip

Satchidanand Haridas sharidas at zeomega.com
Thu Aug 19 02:37:48 EDT 2004


Steven Bethard wrote:

>So I know that zip(*) is the inverse of zip(), e.g.:
>
>  
>
>>>>zip(*zip(range(10), range(10)))
>>>>        
>>>>
>[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)]
>
>What's the inverse of izip?  Of course, I could use zip(*) or izip(*),
>e.g.:
>
>  
>
>>>>zip(*itertools.izip(range(10), range(10)))
>>>>        
>>>>
>[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)]
>  
>
>>>>x, y = itertools.izip(*itertools.izip(range(10), range(10)))
>>>>x, y
>>>>        
>>>>
>((0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
>
>But then I get a pair of tuples, not a pair of iterators.  Basically,
>I want to convert an iterator of tuples into a tuple of iterators.
>
>Steve
>  
>
---------------------------------

 >>> a = itertools.izip(*itertools.izip(range(10),range(10)))
 >>> a
<itertools.izip object at 0x40164f2c>

 >>>a.next()
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
 >>> a.next()
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
 >>> a.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
StopIteration

-----------------------------


Regards,
Satchit


----
Satchidanand Haridas (sharidas at zeomega dot com)

ZeOmega (www.zeomega.com)
Open  Minds' Open Solutions

#20,Rajalakshmi Plaza,
South End Road,
Basavanagudi,
Bangalore-560 004, India






More information about the Python-list mailing list