Assume that I have the following list of lists:
a = []
for i in range(500):
a.extend([list(np.random.randn(50))])
If I was to add lists of the list a
to another list I could use either +
or extend
. I make the assumption that +
and +=
should have the same speed but it seems to be not the case. Here are my codes and the results:
%%timeit
b = []
for number in a:
b = b + [number]
-------------------
321 µs ± 3.11 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%%timeit
b = []
for number in a:
b += [number]
-------------------
26.4 µs ± 117 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Why there is such a huge difference in performance?