[Python-ideas] Proposal how to remove all occurrences of a value from a Python list
Steven D'Aprano
steve at pearwood.info
Wed Oct 7 04:02:04 CEST 2015
On Tue, Oct 06, 2015 at 09:06:40PM -0700, Emil Rosendahl Petersen wrote:
> 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?
Yes, the intent is obvious, because I am a sentient human who can read
the English text and guess -- I emphasis that is it just a guess -- what
you want. Do you believe that Python should do the same thing that I
did? Read the individual strings, analyse them as English text, and
understand that because the final item says "on their own line" that is
your intent.
But what if I wrote this instead:
print(['one string per line', 'three strings per line',
'everything on one line', 'nah just kidding',
'print using two equal-spaced columns'])
What is my intention now? If *you* can't guess what I want, how can the
interpreter guess?
Do I want the following printed one number per line or all numbers on
one line? Should I see the list delimiters? Should the output be
formatted into multiple columns? How many columns? Should each number be
left-justified, right-justified? Centered?
print([1, 2, 3, 4, 5, 6, 7, 8, 998, 999])
Trying to have a programming language intuit the programmers *intent* is
a fool's errand: it cannot be done successfully. Computers cannot do
what we want, they can only do what we tell them to do.
> "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?
No, of course not. If you think it is obvious, you haven't thought about
it in enough detail.
What happens if the replacement strings overlap?
What happens if the new string contains one or more of the old strings
as substrings?
Should the order of the old strings make a difference to the final
result?
Why a list? Isn't that likely to indicate a programming error?
If replace() took multiple target substrings to be replaced, I have
answers to those questions. But I don't know if those answers are the
same as your answers. Maybe they are, maybe they're not. Who knows?
- overlapping target strings shouldn't make a difference;
- neither should the replacement string containing one or more of the
targets;
- or the order of the targets;
- but a list probably means a programming error, I would prefer to
require a tuple of substrings to be consistent with other string
methods, and to avoid any questions of what happens with arbitrary
iterables.
--
Steve
More information about the Python-ideas
mailing list