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

Steven D'Aprano steve at
Thu Oct 18 05:02:58 CEST 2007

On Wed, 17 Oct 2007 21:40:40 +0000, Paul Hankin wrote:

> On Oct 17, 10:03 pm, Debajit Adhikary <debaj... at> 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.


More information about the Python-list mailing list