Deleting the first element of a list

Delaney, Timothy tdelaney at
Thu Oct 3 03:12:02 CEST 2002

> From: Greg Ewing [mailto:see_reply_address at something.invalid]
> 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.

One reason why there may not be any perceptable difference is that del x[0]
will need to move all the rest of the data in the current implementation
i.e. you have:

    del x[0]:
        Move x[1:] to x[:-1]

    x = x[1:]
        Create new list
        Copy x[1:] into new list

So basically, do whichever feels more natural to you.

The real advantage of

    x = x[1:]

is  that it won't blow up if x is empty. Of course, you may also *want* it
to blow up in that case.

Tim Delaney

More information about the Python-list mailing list