Reputation: 37
I have an array a
. I want to create new array with doubled size where items is x*2 and x*3.
For example: a = [1,10,100]
result must be b = [2,3,20,30,200,300]
I know this (ugly and very slow) way: b = sum([[x*2,x*3] for x in a], [])
There is other way (truly I want shortest way :)?
Upvotes: 1
Views: 63
Reputation: 46603
This can be done using a list comprehension with nested loops
In [4]: [y for x in a for y in (x * 2, x * 3)]
Out[4]: [2, 3, 20, 30, 200, 300]
Seems to outperform all answers, but loses to the numpy
solution when a
is large.
Upvotes: 3
Reputation: 4989
Generally using tuples are faster than list:
>>> timeit.timeit("sum([[x*2,x*3] for x in (1,10,100)], [])", number=10000)
0.023060083389282227
>>> timeit.timeit("sum(((x*2,x*3) for x in (1,10,100)), ())", number=10000)
0.01667189598083496
Upvotes: 0
Reputation: 18551
You can do this several ways. Below is one of them, using numpy (line 4):
In [1]: import numpy as np
In [2]: a = [1, 10, 100]
In [3]: %timeit sum([[x*2,x*3] for x in a], [])
1000000 loops, best of 3: 632 ns per loop
In [4]: %timeit x = np.array(a); np.array([x*2,x*3]).T.ravel()
100000 loops, best of 3: 3.25 µs per loop
Your way is faster! But this is because a
is small. When it's larger, numpy becomes much better.
In [5]: a = range(1000)
In [6]: %timeit sum([[x*2,x*3] for x in a], [])
100 loops, best of 3: 2.37 ms per loop
In [7]: %timeit x = np.array(a); np.array([x*2,x*3]).T.ravel()
10000 loops, best of 3: 39.6 µs per loop
Included timeit results for @CoryKramer's answer above, which is fastest for small arrays but also loses to numpy for large arrays:
In [10]: a = [1, 10, 100]
In [11]: %timeit [j for i in zip([i*2 for i in a], [i*3 for i in a]) for j in i]
1000000 loops, best of 3: 853 ns per loop
In [12]: a = range(1000)
In [13]: %timeit [j for i in zip([i*2 for i in a], [i*3 for i in a]) for j in i]
1000 loops, best of 3: 252 µs per loop
Upvotes: 0
Reputation: 118021
You can perform the multiplications in a list comprehension, then zip
and flatten the resulting lists.
>>> a = [1,10,100]
>>> b = [j for i in zip([i*2 for i in a], [i*3 for i in a]) for j in i]
>>> b
[2, 3, 20, 30, 200, 300]
Upvotes: 2