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


Groups > comp.lang.python > #82078 > unrolled thread

Re: Tuple of lists concatenation - function vs comprehension

Started byTerry Reedy <tjreedy@udel.edu>
First post2014-12-07 17:21 -0500
Last post2014-12-09 06:16 -0800
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#82078 — Re: Tuple of lists concatenation - function vs comprehension

FromTerry Reedy <tjreedy@udel.edu>
Date2014-12-07 17:21 -0500
SubjectRe: Tuple of lists concatenation - function vs comprehension
Message-ID<mailman.16687.1417990918.18130.python-list@python.org>
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.

> BUT when I check L itself I'll see this one
>
>>>> L
> [1,2,3,5,6]


-- 
Terry Jan Reedy

[toc] | [next] | [standalone]


#82199

FromRustom Mody <rustompmody@gmail.com>
Date2014-12-09 06:16 -0800
Message-ID<2b804c65-cfd7-4cd1-80c9-1d09bf6b314b@googlegroups.com>
In reply to#82078
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web