Deleting the first element of a list

Peter Hansen peter at engcorp.com
Wed Oct 2 19:25:59 EDT 2002


Erik Max Francis wrote:
> JB wrote:
> 
> 
>>Is using del to delete the first element (that is, the 0th
>>element) of a list faster than x = x[1:]?
>  
> The short answer, though, is not to optimize prematurely.  That's the
> First Rule of Optimization:  

Actually, that's the first rule of *programming*. :-)

 > Don't bother optimizing until you have
> quantitative information about where the bottlenecks truly are.  This
> concept is especially powerful in a high-level language like Python; the
> very choice of using Python means that you're willing to trade raw speed
> for flexibility.  If you're overconcerned with squeezing every last bit
> of CPU power right from the start, then Python (or any other high-level
> language) was probably not a good choice of languages.

Well stated, although a note about changing algorithms generally
providing the most significant speedups might be in order.  Even
with Python, picking a better algorithm can produce huge gains.
(This does not, of course, apply to a small idiomatic choice such
as this thread was about.)

-Peter




More information about the Python-list mailing list