Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to get the nth element of each tuple from a list of tuples in Python

Tags:

python

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.

like image 904
asmeurer Avatar asked Sep 15 '12 19:09

asmeurer


People also ask

How do you extract the n th elements from a list of tuples?

To extract the n-th elements from a list of tuples with Python, we can use list comprehension. We get the n-th element from each tuple and put them into a list with [x[n] for x in elements] . x is the tuple being retrieved. Therefore, e is [1, 3, 5] .

How do you access each element in a list of tuples?

In the majority of programming languages when you need to access a nested data type (such as arrays, lists, or tuples), you append the brackets to get to the innermost item. The first bracket gives you the location of the tuple in your list. The second bracket gives you the location of the item in the tuple.


1 Answers

You can use a list comprehension

[x[0] for x in G]

or operator.itemgetter()

from operator import itemgetter
map(itemgetter(0), G)

or sequence unpacking

[x for x, y, z in G]

Edit: Here is my take on timing the different options, also in Python 3.2:

from operator import itemgetter
import timeit

G = list(zip(*[iter(range(30000))] * 3))

def f1():
    return [x[0] for x in G]
def f2():
    return list(map(itemgetter(0), G))
def f3():
    return [x for x, y, z in G]
def f4():
    return list(zip(*G))[0]
def f5():
    c0, *rest = zip(*G)
    return c0
def f6():
    c0, c1, c2 = zip(*G)
    return c0
def f7():
    return next(zip(*G))

for f in f1, f2, f3, f4, f5, f6, f7:
    print(f.__name__, timeit.timeit(f, number=1000))

Results on my machine:

f1 0.6753780841827393
f2 0.8274149894714355
f3 0.5576457977294922
f4 0.7980241775512695
f5 0.7952430248260498
f6 0.7965989112854004
f7 0.5748469829559326

Comments:

  1. I used a list with 10000 triples to measure the actual processing time, and make function call overhead, name lookups etc. negligible, which would otherwise seriously influence the results.

  2. The functions return a list or a tuple – whatever is more convenient for the particular solution.

  3. Compared to the wolf's answer, I removed the redundant call to tuple() from f4() (the result of the expression is a tuple already), and I added a function f7() which only works to extract the first column.

As expected, the list comprehensions are fastest, together with the somewhat less general f7().

Another edit: Here are the results for ten columns instead of three, with the code adapted where appropriate:

f1 0.7429649829864502
f2 0.881648063659668
f3 1.234360933303833
f4 1.92038893699646
f5 1.9218590259552002
f6 1.9172680377960205
f7 0.6230220794677734
like image 122
Sven Marnach Avatar answered Oct 12 '22 10:10

Sven Marnach