I had some code that contained zip(*G)[0] (and elsewhere, zip(*G)[1], with a different G). G is a list of tuples. What this does is return a list of the first (or generally, for zip(*G)[n], the n-1th) element of each tuple in G as a tuple. For example,
>>> G = [(1, 2, 3), ('a', 'b', 'c'), ('you', 'and', 'me')]
>>> zip(*G)[0]
(1, 'a', 'you')
>>> zip(*G)[1]
(2, 'b', 'and')
This is pretty clever and all, but the problem is that it doesn’t work in Python 3, because zip is an iterator there. Furthermore, 2to3 isn’t smart enough to fix it. So the obvious solution is to use list(zip(*G))[0], but that got me thinking: there is probably a more efficient way to do this. There is no need to create all the tuples that zip creates. I just need the nth element of each tuple in G.
Is there are more efficient, but equally compact way to do this? I’m OK with anything from the standard library. In my use case, each tuple in G will be at least length n, so there is no need to worry about the case of zip stopping at the smallest length tuple (i.e., zip(*G)[n] will always be defined).
If not, I guess I’ll just stick with wrapping the zip in list().
(P.S., I know this is unnecessary optimization. I’m just curious is all)
UPDATE:
In case anyone cares, I went with the t0, t1, t2 = zip(*G) option. First, this lets me give meaningful names to the data. My G actually consists of length 2 tuples (representing numerators and denominators). A list comprehension would only be marginally more readable than the zip, but this way is much better (and since in most cases the zip was list I was iterating through in a list comprehension, this makes things flatter).
Second, as noted by @thewolf and @Sven Marnach’s excellent answers, this way is faster for smaller lists. My G is actually not large in most cases (and if it is large, then this definitely won’t be the bottleneck of the code!).
But there were more ways to do this than I expected, including the new a, *b, c = G feature of Python 3 I didn’t even know about.
At least the fastest way in Python 2.7 is
Here is the test:
Results:
If you don’t care if the result is a list, a list comprehension if faster.
Here is a more extended benchmark with variable list sizes:
Which produces this plot for smaller data sizes (5 to 35):
And this output for larger ranges (25 to 250):
You can see that
f1, a list comprehension is fastest.f6andf1ttrading places as the fastest to return a tuple.