Python list comprehension, unpacking and multiple operations

Question:

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.

Asked By: arynaq

||

Answers:

Given

>>> import itertools
>>> x = range(3)
>>> y = [(i, j**2) for i, j in zip(x, x)]
>>> y
[(0, 0), (1, 1), (2, 4)]

You can use itertools.chain.from_iterable:

>>> list(itertools.chain.from_iterable(y))
[0, 0, 1, 1, 2, 4]

To skip over the intermediary y:

>>> list(itertools.chain.from_iterable((i, j**2) for i, j in zip(x, x)))
[0, 0, 1, 1, 2, 4]
Answered By: Tim Pietzcker

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)
Answered By: Andrew Clark