[Python-ideas] Proposal how to remove all occurrences of a value from a Python list
Emil Rosendahl Petersen
emilrosendahlpetersen at outlook.com
Wed Oct 7 06:06:40 CEST 2015
On Wed, 2015-10-07 at 11:01 +1100, Chris Angelico wrote:
> On Wed, Oct 7, 2015 at 10:21 AM, Sven R. Kunze <srkunze at mail.de> wrote:
> > 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}]
> >
>
> Maybe, but how many other variants do you need? "remove all elements
> 2.7<x<7.5"? "remove all strings that begin with 'a'"? Every new method
> you create adds cognitive load to everyone who reads the docs for the
> list object, and every new feature of a method adds cognitive load to
> understanding that method. Is it worth it? How common *is* this case?
> Is it really worth having the method? Comprehensions already exist,
> and are already general enough to handle all the variants.
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
Python is supposed to make use of duck-typing. Well... I say the
standard library should apply that philosophy.
If we are supposed to be a dynamic language, why can't we be like perl,
and do list operations if we see lists, and scalar/single operations if
we see single value types?
It really comes down to "quack like a duck", in this case - Is it
possible to loop over each thing in the given list, treat it like a
string, and replace it with the string we apply to each iteration?
I believe it should be.
print(['One thing', 'another thing', 'each of these', 'on their own
line'])
Looking at that... isn't the intent very obvious? Why should python not
respect that?
"A long thing".replace("long ", '')
>>> a thing
"A plyable soft tube an undescribable color.".replace(['soft',
'plyable'], '').replace('undescribable', 'indescribable')
Isn't the behaviour of this code - as it would be, if this worked -
fairly obvious?
Frankly to me it seems much more readable and step-by-step to me than a
comparable comprehension. What actually physically happens in the code
is easily apparently, unlike in a comprehension, at least until you take
in its local scope variables.
And lets face it, nested comprehensions can be rather messy. Since
Python does not encourage tail calls, there should be a good and
efficient way to apply operations everywhere.
But maybe map is best? I haven't seen anyone in this thread present an
alternative to changing the function - Perhaps some trick of a decorator
or similar.
But there is no obvious syntax, in my opinion, for accomplishing what I
did with the nested .replace calls.
More information about the Python-ideas
mailing list