convert list of tuples into several lists

Nick Coghlan ncoghlan at iinet.net.au
Thu Feb 10 09:27:32 EST 2005


Steven Bethard wrote:
> Peter Hansen wrote:
> 
>> Steven Bethard wrote:
>>
>>> Diez B. Roggisch wrote:
>>>
>>>> zip(*[(1,4),(2,5),(3,6)])
>>>
>>>
>>> While this is also the approach I would use, it is worth noting that 
>>> Guido thinks of this as an abuse of the argument passing machinery:
>>>
>>> http://mail.python.org/pipermail/python-dev/2003-July/037346.html
>>
>>
>> I'm not sure that's the same thread I already read where he
>> dissed zip like that, but what I'm wondering is what is the
>> alternative?  Is there an equally elegant approach that
>> doesn't "abuse" the argument passing machinery?
> 
> 
> I know I found it in another thread before.  I think he's said it a few 
> times.
> 
> Personally, I wish that, if we're not to use zip like this, that Python 
> would provide a builtin 'unzip' to do the corresponding thing.

I never really got the impression that Guido was particularly *strongly* opposed 
to this use of the extended call syntax. Merely that he was concerned that it 
would break down if the relevant list turned out to be large (that is, the abuse 
is using *args with a list when the list may turn out to be large, not a problem 
specifically with using the star syntax with zip()).

Maybe he's been more explicit somewhere, and I just never saw it.

Anyway, his concern seems justified, as my understanding is that func(*iterable) 
is roughly equivalent to func(*tuple(iterable)), which can be rather expensive 
when the iterable is a long list of tuples.

So zip(*zip(*iterable)) is actually zip(*tuple(zip(*tuple(iterable)))). That's 
potentially an awful lot of data copying for an identity operation :)

Anyway, I think it does make a decent case for an itertools.iunzip or some such 
beast.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list