[Python-ideas] Python Enhancement Proposal for List methods

Chris Angelico rosuav at gmail.com
Sun Oct 21 19:31:42 EDT 2018


On Mon, Oct 22, 2018 at 7:58 AM Terry Reedy <tjreedy at udel.edu> wrote:
>
> An indefinite number of in-place removes is a generally a bad idea
> unless done carefully.
> alist.remove is inherently O(n).
> alist.removeall implemented naively would be O(n*n)/
> alist = [x for x in alist if x == old] or
> alist = list(x for x in alist if x == old) or
> alist = list(filter(lambda x: x == old)) is O(n).
>

That's actually a good reason to make it part of the language or
stdlib - it's very easy to get it wrong. All of the above are not
in-place. If I were to make a stdlib function to do removeall, it
would be:

seq[:] = [x for x in seq if x != removeme]

(And that one probably missed some subtlety somewhere too.) So it'd be
less buggy for people to reach for a standard solution than to try to
craft their own.

That said, though, I think this probably belongs as a recipe, not as a
stdlib function or method.

ChrisA


More information about the Python-ideas mailing list