a.extend(b) better than a+=b ?

Alf P. Steinbach alfps at start.no
Thu Apr 22 03:16:51 EDT 2010


* candide:
> Suppose a and b are lists.
> 
> What is more efficient in order to extend the list a by appending all 
> the items in the list b ?
> 
> 
> I imagine a.extend(b)to be more efficient for only appendinding the 
> items from b while a+=b creates a copy of a before appending, right ?

No.

But in general, if you're concerned about efficiency, *measure*.

And keep in mind that measurement results may only be applicable to a given 
Python implementation, for given data, in a given environment...

In most cases you'll do better by focusing on algorithmic efficiency rather than 
low level operational efficiency.

Also keep in mind that when or if operational efficiency becomes an issue, then 
for Python the answer is in general to use functionality (modules) implemented 
in other languages. Python is not designed with operational efficiency as a 
goal. It's designed as an easy language.


Cheers & hth.,

- Alf



More information about the Python-list mailing list