Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82062
| From | "Ivan Evstegneev" <webmailgroups@gmail.com> |
|---|---|
| References | <001a01d0122f$86b95480$942bfd80$@gmail.com> <20141207151749.GB32773@FuzzyHorror> |
| Subject | RE: Tuple of lists concatenation - function vs comprehension |
| Date | 2014-12-07 17:28 +0200 |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16673.1417966680.18130.python-list@python.org> (permalink) |
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] BUT when I check L itself I'll see this one >>>L [1,2,3,5,6] Ok. Thanks. -----Original Message----- From: Shiyao Ma [mailto:i@introo.me] Sent: Sunday, December 7, 2014 17:18 To: Ivan Evstegneev Cc: python-list@python.org Subject: Re: Tuple of lists concatenation - function vs comprehension 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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
RE: Tuple of lists concatenation - function vs comprehension "Ivan Evstegneev" <webmailgroups@gmail.com> - 2014-12-07 17:28 +0200
csiph-web