Search code examples
sum

(Python)Why the sum of list of list and [] is a list?


Why the result of code below is [1, 2, 3, 4, 5]?

In[10]:=sum([[1,2,3], [4,5]],[])
Out[10]:=[1,2,3,4,5]  # why the result is [1, 2, 3, 4, 5]?

The purpose is to get item of all the list in the list [[1,2,3], [4,5]]. (I don't know why "[]" in the second argument of sum.)


Solution

  • Since

    sum([1, 2, 3], 0)
    

    means 0 + 1 + 2 + 3 ,

    then

    sum([[1, 2, 3], [4, 5]], [])
    

    means: [] + [1, 2, 3] + [4, 5]