[Tutor] why is list.pop(0) slow? [swap-with-last-element trick]

Magnus Lyckå magnus@thinkware.se
Wed Jun 18 21:23:03 2003


At 20:22 2003-06-18 -0400, Gus Tabares wrote:
>I know I missed Wari's original e-mail to the list, but what about this:
>
> >>> def removeFront(list):
>         item = list[0]
>         del list[0]
>         return item

It's just as slow as l.pop(0), and for the same reason. When
you do "del list[0]" you reshuffle the entire list.

Wari says "8 times slower", but that depends on the size of
the list, the bigger the list, the bigger the time diff.

If this is a problem, you might want to use another structure
than a list. We can't really saw what without seeing what you
are trying to do...


--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language