Efficiently Split A List of Tuples

Raymond Hettinger python at rcn.com
Sun Jul 17 22:18:23 EDT 2005


> Variant of Paul's example:
>
> a = ((1,2), (3, 4), (5, 6), (7, 8), (9, 10))
> zip(*a)
>
> or
>
> [list(t) for t in zip(*a)] if you need lists instead of tuples.


[Peter Hansen]
> (I believe this is something Guido considers an "abuse of *args", but I
> just consider it an elegant use of zip() considering how the language
> defines *args.  YMMV]

It is somewhat elegant in terms of expressiveness; however, it is also
a bit disconcerting in light of the underlying implementation.

All of the tuples are loaded one-by-one onto the argument stack.  For a
few elements, this is no big deal.  For large datasets, it is a less
than ideal way of transposing data.

Guido's reaction makes sense when you consider that most programmers
would cringe at a function definition with thousands of parameters.
There is a sense that this doesn't scale-up very well (with each Python
implementation having its own limits on how far you can push this
idiom).



Raymond




More information about the Python-list mailing list