This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: "replace" function should accept lists.
Type: enhancement Stage:
Components: None Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: loewis, poromenos, rhettinger
Priority: normal Keywords:

Created on 2005-04-17 16:05 by poromenos, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (12)
msg54431 - (view) Author: Poromenos (poromenos) Date: 2005-04-17 16:05
It would be nice if the "replace" function accepted lists/
tuples and replaced each item in the "old" list with the 
corresponding item in the "new" list.
msg54432 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-04-18 21:11
Logged In: YES 
user_id=80475

Are you requesting that lists be given a replace() method
that parallels the replace() method for strings?

def replace(self, old, new):
    result = []
    for elem in self:
        if elem == old:
            result.append(new)
        else:
            result.append(elem)
    self[:] = result
msg54433 - (view) Author: Poromenos (poromenos) Date: 2005-04-18 21:15
Logged In: YES 
user_id=267121

Hmm, actually I was requesting that string.replace() accepted 
lists of substitutions, like so:
>>> "Hello world".replace(["e", "d"], ["a", "e"])
'Hallo worle'

But your suggestion would also be very useful.
msg54434 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-04-18 22:20
Logged In: YES 
user_id=80475

I'm -1 on complicating str.replace()'s API for functionality
that substantially duplicates that offered by str.translate():

>>> "Hello world".translate(string.maketrans('ed', 'ae'))
'Hallo worle'
msg54435 - (view) Author: Poromenos (poromenos) Date: 2005-04-18 22:23
Logged In: YES 
user_id=267121

Ah, I did not know that... The docs are a bit complicated on .
translate, but since it can do that, yes, it would be unwise to 
implement my suggestion.
msg54436 - (view) Author: Poromenos (poromenos) Date: 2005-04-29 13:03
Logged In: YES 
user_id=267121

There was an oversight on my part... Translate can only be 
used to change individual characters, what I am proposing 
could replace strings of multiple characters, essentially 
concatenating multiple replace()s in one:
>>> "h.w".replace(["h", ".", "w"], ["Hello", " ", "World!"])
'Hello, World!'
msg54437 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-04-29 14:10
Logged In: YES 
user_id=80475

Given the multiple alternative input matches, this is a job
for regular expressions.  See the string.substitute() source
code for an approach to making the transformation you outlined.

I do not think multi-replace is sufficiently common to
warrant a change to the API.  If needed and if the regexp
solution is too hard, then a regular for-loop is a
reasonable alternative:

    for old, new in zip(oldlist, newlist):
         s = s.replace(old, new)
msg54438 - (view) Author: Poromenos (poromenos) Date: 2005-04-30 09:48
Logged In: YES 
user_id=267121

That is true, the alternative loop is quite usable, but the API 
change would be backwards-compatible, and the 
implementation is not very difficult... I just see this as a nice 
feature of replace, it's not really necessary if it'll break other 
stuff.
msg54439 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-04-30 12:30
Logged In: YES 
user_id=80475

Sorry, am not going to gum up the API for this.  It doesn't
come up much and when it does it is an easy problem.

Each additional option on a function makes it harder to
learn, use, and remember.  Likewise, it complicates maintenance.

Also, this one has its own complexities that make it worth
avoiding (examples can be contrived when the for-loop
version produces a different result than replacing each
match as found).

A good implementation would take time.  It would involve
regexps and have to be done for unicode objects.
msg54440 - (view) Author: Poromenos (poromenos) Date: 2005-05-01 09:55
Logged In: YES 
user_id=267121

Ah, okay.
msg54441 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2005-05-20 21:48
Logged In: YES 
user_id=21627

I'm rejecting this request, noticing that even the
*specification* of the requested feature is tricky:

Would

"foobarfoo".replace([("foo","baz"), ("barf", "")]

produce

"bazbarbaz"

or

"bazfoo"

IOW, would it first replace all occurrences of the first
pair, then all occurrences of the second pair, or would it
iterate over the list, replacing everything it can replace,
and then start all over until nothing needs to be done?

In the face of doubt, refuse the temptation to guess.
msg54442 - (view) Author: Poromenos (poromenos) Date: 2005-05-20 21:52
Logged In: YES 
user_id=267121

Actually it would accept two lists, and replace item n in the 
first list with item n in the second list, in that order (so that 
you can condense multiple replace lines into one), but this 
feature is not that important anyway.
History
Date User Action Args
2022-04-11 14:56:10adminsetgithub: 41870
2005-04-17 16:05:38poromenoscreate