Python list comprehension, unpacking and multiple operations

arynaq picture arynaq · Dec 19, 2012 · Viewed 23.6k times · Source

I want to unpack the tuples I create by doing the following so he the result is just one simple list. I can get the desired result in 2-3 lines but surely there is a oneliner list.comp?

x = range(10)
y = [(i,j**2) for i,j in zip(x,x)]
>>>y
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49), (8, 64), (9
, 81)]
>>>

What I want is result = [0,0,1,1,2,4,3,9.....]

Doing

y = len(x)*[0]
y[::2] = x
y[1::2] = [i**2 for i in x]

Gives what I want but what if I need the more general case:

y = [(i, sqrt(i), i**3, some_operation_on_i, f(i), g(i)) for i in x]

Eg I should be able to get a straight list like result where I only specified one operation (square) to follow each i but now with an arbitrary number of operations following each i.

Answer

Andrew Clark picture Andrew Clark · Dec 19, 2012

Use a nested list comprehension:

result = [a for tup in y for a in tup]

Example:

>>> x = range(10)
>>> y = [(i,j**2) for i,j in zip(x,x)]
>>> [a for tup in y for a in tup]
[0, 0, 1, 1, 2, 4, 3, 9, 4, 16, 5, 25, 6, 36, 7, 49, 8, 64, 9, 81]

This will work fine for your more general case as well, or you could do it all in one step:

y = [a for i in x for a in (i, sqrt(i), i**3, some_operation_on_i, f(i), g(i))]

In case the nested list comprehensions look odd, here is how this would look as a normal for loop:

y = []
for i in x:
    for a in (i, sqrt(i), i**3, some_operation_on_i, f(i), g(i)):
        y.append(a)