[Python-ideas] Allow multiple arguments to `list.remove` and flag for silent fail

Joshua Landau joshua at landau.ws
Tue Aug 27 00:23:28 CEST 2013


On 26 August 2013 22:07, Sébastien Volle <sebastien.volle at gmail.com> wrote:
> List comprehension works too:
>
> my_list = [ thing for thing in my_list if thing not in things ]
>
> But both our solutions don't change my_list in place but require creating a
> new list, and would not be very practical with big lists.

Aside from the fact that using a list here is basically a stupid idea,
the analysis is as such:

    my_list = [ thing for thing in my_list if thing not in things ]

has time complexity O(n * m) where n is the length of my_list and m is
the length of things, unless things is made a set in which it would be
O(n).
It has space complexity O(n).

    for thing in things:
        try:
            my_list.remove(thing)
        except ValueError:
           pass

has time complexity O(n * m) and space complexity O(1).

So they're both terrible.


More information about the Python-ideas mailing list