Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82199

Re: Tuple of lists concatenation - function vs comprehension

Newsgroups comp.lang.python
Date 2014-12-09 06:16 -0800
References <001a01d0122f$86b95480$942bfd80$@gmail.com> <20141207151749.GB32773@FuzzyHorror> <001b01d01232$77700840$665018c0$@gmail.com> <mailman.16687.1417990918.18130.python-list@python.org>
Message-ID <2b804c65-cfd7-4cd1-80c9-1d09bf6b314b@googlegroups.com> (permalink)
Subject Re: Tuple of lists concatenation - function vs comprehension
From Rustom Mody <rustompmody@gmail.com>

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Re: Tuple of lists concatenation  -  function vs comprehension Terry Reedy <tjreedy@udel.edu> - 2014-12-07 17:21 -0500
  Re: Tuple of lists concatenation  -  function vs comprehension Rustom Mody <rustompmody@gmail.com> - 2014-12-09 06:16 -0800

csiph-web