[Python-ideas] Proposal how to remove all occurrences of a value from a Python list

Sven R. Kunze srkunze at mail.de
Wed Oct 7 01:21:10 CEST 2015


On 07.10.2015 00:50, Chris Angelico wrote:
> On Wed, Oct 7, 2015 at 3:29 AM, Sven R. Kunze <srkunze at mail.de> wrote:
>> Btw. one could also think of an additional generalization for this that
>> basically takes n arguments which are then removed altogether from the list
>> in question:
>>
>> a = [1,2,3,5,4,2,3,4,1,2]
>> a.remove_all(1,4,2)
>> a == [3,5,3]
>>
>> Just thinking, and I might remember some time where I would have found it
>> useful. Not sure.
>>
> The more generalizations you offer, the more tempting the
> comprehension looks.

To you, not to everybody.

> It already allows as many generalizations as you
> like, because comprehensions are a fully-supported part of the
> language.

You miss the point. Common use-cases deserve methods on their own. 
People will never stop asking for meaningfully named methods, even when 
pretending comprehensions are the ultimate answer to all questions 
concerning lists/sets/dicts, They simply are not.

Which variant conveys the intent of the developer more clearly?

a.remove_all(1,4,2)
a[:] = [x for x in a if x not in {1,4,2}]

I have to admit, the latter variant has certain appeal if you love 
special characters.

Best,
Sven


More information about the Python-ideas mailing list