Deleting the first element of a list

Greg Ewing see_reply_address at something.invalid
Thu Oct 3 02:48:30 CEST 2002


JB wrote:

> Is using del to delete the first element (that is, the 0th 
> element) of a list faster than x = x[1:]?


Probably, because del x[0] modifies the list in-place,
whereas x = x[1:] makes a copy of the whole list
except for the first element.

I say "probably" because intuitions about this sort
of thing are sometimes wrong in Python. The only
way to find out for sure is by experiment.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list