When I want to unfold a list, I found a way like below:
>>> a = [[1, 2], [3, 4], [5, 6]]
>>> a
[[1, 2], [3, 4], [5, 6]]
>>> sum(a, [])
[1, 2, 3, 4, 5, 6]
I don't know what happened in these lines, and the documentation states:
sum(iterable[, start])
Sums
start
and the items of aniterable
from left to right and returns the total.start
defaults to0
. The iterable's items are normally numbers, and thestart
value is not allowed to be a string.For some use cases, there are good alternatives to
sum()
. The preferred, fast way to concatenate a sequence of strings is by calling''.join(sequence)
. To add floating point values with extended precision, seemath.fsum()
. To concatenate a series of iterables, consider usingitertools.chain()
.New in version 2.3.
Don't you think that start should be a number? Why can []
be written here?
(sum(a, []))