[Python-ideas] Allow multiple arguments to `list.remove` and flag for silent fail
Andrew Barnert
abarnert at yahoo.com
Tue Aug 27 01:19:16 CEST 2013
On Aug 26, 2013, at 15:23, Joshua Landau <joshua at landau.ws> wrote:
> 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.
In practical terms, except for the edge cases of m being near 0 or near n, in-place removing is almost always significantly slower.
In general, if you're filtering a list in-place, you probably shouldn't be. If you're doing it in-place because it's a shared value, just use my_list[:]= instead of my_list=. If you're doing it for atomicity reasons, it doesn't work. If you're doing it as an optimization because the equivalent would be faster in, say, C++, it's probably a pessimization. If you're doing it for memory savings, the fact that you're likely to end up with O(m) wasted permanent storage (because shrinking a list usually doesn't resize it, but making a new list and releasing the old obviously does) is often more important than the O(n-m) temporary storage.
(Of course half the time, you can just turn the listcomp into a genexpr and get the best of both worlds, but obviously that isn't always appropriate.)
More information about the Python-ideas
mailing list