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


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

Re: Tuple of lists concatenation - function vs comprehension

Started byShiyao Ma <i@introo.me>
First post2014-12-07 23:17 +0800
Last post2014-12-07 23:17 +0800
Articles 1 — 1 participant

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 Shiyao Ma <i@introo.me> - 2014-12-07 23:17 +0800

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

FromShiyao Ma <i@introo.me>
Date2014-12-07 23:17 +0800
SubjectRe: Tuple of lists concatenation - function vs comprehension
Message-ID<mailman.16671.1417965483.18130.python-list@python.org>
On 12/07, Ivan Evstegneev wrote:
> Hello,
>
> When I have worked  in Python shell (IDLE) I found this issue:
>
> >>>x = ([1, 2], [3, 4], [5, 6])
> >>>L = []
> >>>for I in x:
> 	L.extend(i)
>
> >>>L
> [1,2,3,4,5,6]
>
> But when I try to make comprehension using above expression, I get this:
>
> >>>x = ([1, 2], [3, 4], [5, 6])
> >>>L = []
> >>> [L.extend(i) for i in x]
> [None, None, None]

Yes. per the doc, list.extend() returns None.

>
> But this works fine within function:
>
> >>> def myfunc():
> 	x = ([1, 2], [3, 4], [5, 6])
> 	L = []
> 	[L.extend(i) for i in x]
> 	print(L)
>
> >>>myfunc()
> [1, 2, 3, 4, 5, 6]

This is also so true, as you are print the var 'L'.

>
> The question is why I'm getting empty list while working with comprehension
> in interactive console?

You are also getting [None]*3 in comprenhension inside a function.

-- 
Shiyao Ma
http://introo.me

[toc] | [standalone]


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


csiph-web