Delete all items in the list

Chris Rebert clp2 at
Fri Feb 27 08:46:47 EST 2009

On Fri, Feb 27, 2009 at 5:10 AM, Steve Holden <steve at> wrote:
> Chris Rebert wrote:
>> On Thu, Feb 26, 2009 at 10:26 PM, odeits <odeits at> wrote:
> [...]
>>> while 'a' in L:
>>>   L.remove('a')
>>> not the most efficient but it works
>> "Not the most efficient"; it's terribly inefficient! It's 2*O(M*N) [M
>> = number of 'a's in L], versus just N. And that's not even accounting
>> for all the extra element movement done by .remove()
> Surely 2*O(M*N) is O(M*N) since constant factors are irrelevant in
> assessing performance order?

Obviously that equivalence is true, but in this case I'm emphasizing
that it's even worse than that when constant factors are taken into
account. Big-O is nice in the abstract, but in the real-world those
constant factors can matter.

In pure big-O, it is indeed O(M*N) vs. O(N)
Including constant factors, the performance is roughly 2*M*N*X [X =
overhead of remove()] vs. N, which makes the badness of the algorithm
all the more apparent.


Follow the path of the Iguana...

More information about the Python-list mailing list