Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Appending a list's elements to another list using a list comprehension

Reply
Thread Tools

Appending a list's elements to another list using a list comprehension

 
 
Debajit Adhikary
Guest
Posts: n/a
 
      10-17-2007
I have two lists:

a = [1, 2, 3]
b = [4, 5, 6]

What I'd like to do is append all of the elements of b at the end of
a, so that a looks like:

a = [1, 2, 3, 4, 5, 6]

I can do this using

map(a.append, b)

How do I do this using a list comprehension?

(In general, is using a list comprehension preferable (or more
"pythonic") as opposed to using map / filter etc.?)

 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      10-17-2007
On Wed, 17 Oct 2007 20:27:14 +0000, Debajit Adhikary wrote:

> I have two lists:
>
> a = [1, 2, 3]
> b = [4, 5, 6]
>
> What I'd like to do is append all of the elements of b at the end of
> a, so that a looks like:
>
> a = [1, 2, 3, 4, 5, 6]
>
> I can do this using
>
> map(a.append, b)


This is a bad idea as it creates a useless list of `None`\s, one for each
element in `b`.

> How do I do this using a list comprehension?


Not at all. The obvious solution here is ``a.extend(b)``.

> (In general, is using a list comprehension preferable (or more
> "pythonic") as opposed to using map / filter etc.?)


Some say yes.

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
 
 
 
Carsten Haese
Guest
Posts: n/a
 
      10-17-2007
On Wed, 2007-10-17 at 20:27 +0000, Debajit Adhikary wrote:
> I have two lists:
>
> a = [1, 2, 3]
> b = [4, 5, 6]
>
> What I'd like to do is append all of the elements of b at the end of
> a, so that a looks like:
>
> a = [1, 2, 3, 4, 5, 6]
>
> I can do this using
>
> map(a.append, b)
>
> How do I do this using a list comprehension?


You don't.

> (In general, is using a list comprehension preferable (or more
> "pythonic") as opposed to using map / filter etc.?)


In general, a list comprehension is more Pythonic nowadays, but in your
particular case the answer is neither map nor a list comprehension, it's
this:

a += b

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net


 
Reply With Quote
 
Paul Hankin
Guest
Posts: n/a
 
      10-17-2007
On Oct 17, 9:27 pm, Debajit Adhikary <(E-Mail Removed)> wrote:
> I have two lists:
>
> a = [1, 2, 3]
> b = [4, 5, 6]
>
> What I'd like to do is append all of the elements of b at the end of
> a, so that a looks like:
>
> a = [1, 2, 3, 4, 5, 6]
>
> I can do this using
>
> map(a.append, b)
>
> How do I do this using a list comprehension?
>
> (In general, is using a list comprehension preferable (or more
> "pythonic") as opposed to using map / filter etc.?)


Yes, using a list comprehension is usually more pythonic than using
map/filter. But here, the right answer is:
a.extend(b). The first thing you should always do is check the python
libraries for a function or method that does what you want. Even if
you think you know the library quite well it's still worth checking:
I've lost count of the number of times I've discovered a library
function that does exactly what I wanted.

Anyway, if extend didn't exist, the pythonic version of map(a.append,
b) would be
for x in b:
a.append(x)

Rather than
[a.append(x) for x in b]


List comprehensions and map produce a new list. That's not what you
want here: you're using the side-effect of the append method - which
modifies a. This makes using regular iteration the right idea, because
by using map or a comprehension, you're also constructing a list of
the return values of append (which is always None). You can see this
in the interpreter:

>>> map(a.append, b)

[None, None, None]

>>> a

[1, 2, 3, 4, 5, 6]

HTH

--
Paul Hankin

 
Reply With Quote
 
Debajit Adhikary
Guest
Posts: n/a
 
      10-17-2007
On Oct 17, 4:41 pm, Carsten Haese <(E-Mail Removed)> wrote:
> On Wed, 2007-10-17 at 20:27 +0000, Debajit Adhikary wrote:
> > I have two lists:

>
> > a = [1, 2, 3]
> > b = [4, 5, 6]

>
> > What I'd like to do is append all of the elements of b at the end of
> > a, so that a looks like:

>
> > a = [1, 2, 3, 4, 5, 6]

>
> > I can do this using

>
> > map(a.append, b)

>
> > How do I do this using a list comprehension?

>
> You don't.
>
> > (In general, is using a list comprehension preferable (or more
> > "pythonic") as opposed to using map / filter etc.?)

>
> In general, a list comprehension is more Pythonic nowadays, but in your
> particular case the answer is neither map nor a list comprehension, it's
> this:
>
> a += b
>
> HTH,
>
> --
> Carsten Haesehttp://informixdb.sourceforge.net


Thanks a ton

What in general is a good way to learn about little things like these?
(I'm fairly new to the language)

A google search for 'python list methods" did not turn up the +
operator anywhere for me. Where could I find the official
documentation for built in structures like the list? (I just noticed
that the + operator for lists is mentioned in Beazley's Python
Essential Reference -- in the opening pages, which I didn't look at
when I was writing the earlier code.)

How does "a.extend(b)" compare with "a += b" when it comes to
performance? Does a + b create a completely new list that it assigns
back to a? If so, a.extend(b) would seem to be faster. How could I
verify things like these?

 
Reply With Quote
 
Paul Hankin
Guest
Posts: n/a
 
      10-17-2007
On Oct 17, 10:03 pm, Debajit Adhikary <(E-Mail Removed)> wrote:
> How does "a.extend(b)" compare with "a += b" when it comes to
> performance? Does a + b create a completely new list that it assigns
> back to a? If so, a.extend(b) would seem to be faster. How could I
> verify things like these?


Use a += b rather than a.extend(b): I'm not sure what I was thinking.
Anyway, look at 'timeit' to see how to measure things like this, but
my advice would be not to worry and to write the most readable code -
and only optimise if your code's runnign too slowly.

To answer your question though: a += b is *not* the same as a = a + b.
The latter would create a new list and assign it to a, whereas a += b
updates a in-place.

--
Paul Hankin

 
Reply With Quote
 
Debajit Adhikary
Guest
Posts: n/a
 
      10-17-2007
On Oct 17, 5:40 pm, Paul Hankin <(E-Mail Removed)> wrote:
> To answer your question though: a += b is *not* the same as a = a + b.
> The latter would create a new list and assign it to a, whereas a += b
> updates a in-place.


I know I'm being a little finicky here, but how would someone know
that a+=b is not the same as a=a+b?
Any documentation or reference that mentions this?


> Use a += b rather than a.extend(b): I'm not sure what I was thinking.
> Anyway, look at 'timeit' to see how to measure things like this, but
> my advice would be not to worry and to write the most readable code -
> and only optimise if your code's runnign too slowly.


I understand. Thanks
At the same time, however, as someone new learning the language, I
feel it always helps to know what the best practices and patterns are
at the very outset (at least for someone who chooses to become a good
programmer in that language). I mean, for me it's like this, I just
don't want to get the work done, I would really want to know why I do
something a certain way and not some other way

 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      10-18-2007
Debajit Adhikary wrote:
> On Oct 17, 5:40 pm, Paul Hankin <(E-Mail Removed)> wrote:
>> To answer your question though: a += b is *not* the same as a = a + b.
>> The latter would create a new list and assign it to a, whereas a += b
>> updates a in-place.

>
> I know I'm being a little finicky here, but how would someone know
> that a+=b is not the same as a=a+b?
> Any documentation or reference that mentions this?


http://docs.python.org/ref/augassign.html

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-18-2007
On Wed, 17 Oct 2007 23:46:25 +0000, Debajit Adhikary wrote:

> On Oct 17, 5:40 pm, Paul Hankin <(E-Mail Removed)> wrote:
>> To answer your question though: a += b is *not* the same as a = a + b.
>> The latter would create a new list and assign it to a, whereas a += b
>> updates a in-place.

>
> I know I'm being a little finicky here, but how would someone know that
> a+=b is not the same as a=a+b?
> Any documentation or reference that mentions this?


Have you Read The Fine Manual at the Fine Website?

Try typing "Python documentation" into your favourite search engine, or
try going to http://www.python.org/ which links directly to
http://www.python.org/doc/

Unfortunately, searching for operators is always tricky (try searching
for "*" some day...) but the section you want is here:

http://docs.python.org/ref/augassign.html


Also, it isn't always true that a += b is not the same as a = a+b. It
depends on what a and b are.


--
Steven.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-18-2007
On Wed, 17 Oct 2007 21:40:40 +0000, Paul Hankin wrote:

> On Oct 17, 10:03 pm, Debajit Adhikary <(E-Mail Removed)> wrote:
>> How does "a.extend(b)" compare with "a += b" when it comes to
>> performance? Does a + b create a completely new list that it assigns
>> back to a? If so, a.extend(b) would seem to be faster. How could I
>> verify things like these?

>
> Use a += b rather than a.extend(b): I'm not sure what I was thinking.


Neither am I. Why do you say that?


> Anyway, look at 'timeit' to see how to measure things like this, but my
> advice would be not to worry and to write the most readable code - and
> only optimise if your code's runnign too slowly.


Always good advice, but of course what a person considers "the most
readable code" changes with their experience.


> To answer your question though: a += b is *not* the same as a = a + b.



It might be. It depends on what a and b are.



--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Appending a list using list obtained from a class Prasad, Ramit Python 0 10-25-2012 07:06 PM
yet another list comprehension question Ross Python 8 05-06-2009 03:17 AM
List comprehension in if clause of another list comprehension Vedran Furac( Python 4 12-19-2008 01:35 PM
Removing elements from a list that are elements in another list Adam Hartshorne C++ 2 01-27-2006 07:47 AM
Appending style sheet elements delraydog@gmail.com HTML 3 12-02-2005 02:40 PM



Advertisments