Tuple of lists concatenation - function vs comprehension
Rustom Mody
rustompmody at gmail.com
Tue Dec 9 09:16:02 EST 2014
On Monday, December 8, 2014 3:52:53 AM UTC+5:30, Terry Reedy wrote:
> On 12/7/2014 10:28 AM, Ivan Evstegneev wrote:
> > Hi Shiyao,
> >
> > Now I see, that it was kind of dumb question...
> >
> >>>>> x = ([1, 2], [3, 4], [5, 6])
> >>>>> L = []
> >>>> [L.extend(i) for i in x]
> > [None, None, None]
>
> Using a list comprehension for the expression side-effect, when you do
> not actually want the list produced by the comprehension, is considered
> bad style by many. There is nothing wrong with explicit loops.
Yes loops are ok
If you want a solution along the lines you (OP) are seeking here is one:
>>> from operator import add
>>> reduce(add, [[1,2],[3,4]], [])
[1, 2, 3, 4]
>>>
Notes
1. Terry's suggestion to use (the obvious) loop should be heeded
2. Which will also be more efficient
3. The reason I mention the reduce is that its a part of FP lore:
Every map (and therefore comprehension) can be put into the form of a reduce.
But not the contrary.
More information about the Python-list
mailing list