A multidimensional list like l=[[1,2],[3,4]] could be converted to a 1D one by doing sum(l,[]). How does this happen?
(This doesn’t work directly for higher multidimensional lists, but it can be repeated to handle those cases. For example if A is a 3D-list, then sum(sum(A),[]),[]) will flatten A to a 1D list.)
sumadds a sequence together using the+operator. e.gsum([1,2,3]) == 6. The 2nd parameter is an optional start value which defaults to 0. e.g.sum([1,2,3], 10) == 16.In your example it does
[] + [1,2] + [3,4]where+on 2 lists concatenates them together. Therefore the result is[1,2,3,4]The empty list is required as the 2nd paramter to
sumbecause, as mentioned above, the default is forsumto add to 0 (i.e.0 + [1,2] + [3,4]) which would result in unsupported operand type(s) for +: ‘int’ and ‘list’This is the relevant section of the help for
sum:Note
As wallacoloo comented this is not a general solution for flattening any multi dimensional list. It just works for a list of 1D lists due to the behavior described above.
Update
For a way to flatten 1 level of nesting see this recipe from the itertools page:
To flatten more deeply nested lists (including irregularly nested lists) see the accepted answer to this question (there are also some other questions linked to from that question itself.)
Note that the recipe returns an
itertools.chainobject (which is iterable) and the other question’s answer returns ageneratorobject so you need to wrap either of these in a call tolistif you want the full list rather than iterating over it. e.g.list(flatten(my_list_of_lists)).