Efficiently Split A List of Tuples

Ron Adam rrr at ronadam.com
Mon Jul 18 14:18:00 EDT 2005


Raymond Hettinger wrote:
> [Ron Adam]
> 
>>Currently we can implicitly unpack a tuple or list by using an
>>assignment.  How is that any different than passing arguments to a
>>function?  Does it use a different mechanism?
> 
> 
> It is the same mechanism, so it is also only appropriate for low
> volumes of data:
> 
>     a, b, c = *args             # three elements, no problem
>     f(*xrange(1000000))         # too much data, not scalable, bad idea
> 
> Whenever you get the urge to write something like the latter, then take
> it as cue to be passing iterators instead of unpacking giant tuples.
> 
> 
> Raymond

Ah... that's what I expected.  So it better to transfer a single 
reference or object than a huge list of separated items.  I suppose that 
would be easy to see in byte code.

In examples like the above, the receiving function would probably be 
defined with *args also and not individual arguments.  So is it 
unpacked, transfered to the function, and then repacked.  or unpacked, 
repacked and then transfered to the function?

And if the * is used on both sides, couldn't it be optimized to skip the 
unpacking and repacking?  But then it would need to make a copy wouldn't 
it?  That should still be better than passing individual references.

Cheers,
Ron




More information about the Python-list mailing list