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

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Oct 17 16:36:15 EDT 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



More information about the Python-list mailing list