Paddy wrote: > Rhamphoryncus wrote: > > > > > It's worthwhile to note that the use of + as the concatenation operator > > is arbitrary. It could just have well been | or &, and has no > > relationship with mathematically addition. > > The effect of the string concatenation operator is only secondary. > Secondary to the use of the word sum; and what could be 'reasonably' > concieved as sum's effect on non-numeric types. > > > > It's also worth stressing (not in response to your post, but others) > > that sum([[1],[2],[3]], []) is just as bad as attempting to sum > > strings, both conceptually (it's not mathematical addition) > > Unfortunately, the words sum and summation are linked to other words > that are commonly used to describe what is happening to the numders, > the lists, and the strings. > Words such as accumulate, concatenate, aggregate, collect, assemble, as > well as add.
String concatenation and numeric addition only group together under the most vague of english terms, "putting things together". String concatenation violates many mathematical properties of addition/summation, and simply isn't related. It is unfortunate that many languages (including python) promote the confusion by using + for a "put things together" operator, ie both mathematical addition and string concatenation. > > I believe the prefered method to flatten a list of lists is this: > > > > shallow = [] > > for i in deep: > > shallow.extend(i) > > > > Yes, it's three lines. It's also very easy to read. reduce() and > > sum() are not. > > I'd like to squeeze in the listcomp version, not because it is one line > shorter, but because I, and maybe others prefer short listcomps such as > the folowing: > > shallow = [] > [shallow.extend(i) for i in deep] I'm sure this has been mentioned before, but listcomps are for when you want to store the list and use it for further things, not for when you want a side effect. TOOWTDI. And of course, if saving a line was the reason: shallow = [] for i in deep: shallow.extend(i) -- http://mail.python.org/mailman/listinfo/python-list