sum( ([f(x),g(x)] for x in range(n)), [] )
This is equivalent to [f(1),g(1)] + [f(2),g(2)] + [f(3),g(3)] + ...
You can also think of it as:
def flatten(list): ... flatten( [f(x),g(x)] for x in ... )
Note. The correct way is to use itertools.chain.from_iterable or recognize a double list. (This does not require recreating the list on each +, so it has O (N) performance, not O (N ^ 2).) I will still use sum(..., []) when I need a quick one-line or I am in a hurry or when the number of terms is limited (for example, <= 10). That is why I still mention it here, with this warning. You can also use tuples: ((f(x),g(x)) for ...), () (or for a comment on a hacker that has a fg (x) generator that gives a two-tuple).
ninjagecko Aug 08 2018-12-12T00: 00Z
source share