sometimes you should have nested merge to merged list(it is similar to np.flatten()
).
when the list of list is given like below, and you should flatten it
a = [[j for j in range(0, 10)] for i in range(0, 10000)]
you have two kinds of solution to solve it. itertools.chain.from_iterable
and functools.reduce
.
%timeit list(itertools.chain.from_iterable(a))
%timeit reduce(lambda x, y: x+y, a)
do you think which one is faster and how much faster than other thing?
itertools.chain.from_iterable
is 1000 times faster or more(when the length of the list is bigger).
If somebody knows why this thing happen, please let me know.
always thx for you support and help.
chain
iterator as-is. – abarnert Mar 7 '18 at 10:27