0

I am trying to replace substrings to nothing. For example, I want to convert £1,000.90 to 1000.90 i.e. simply replace '£' and ',' with nothing. And I thought I will do this using the inline for loop.

s = '£1,000.90'
replaceStrings = ['£', ',']
s = (s.replace(x, '') for x in replaceStrings)
print(s)

But it doesn't work. I find many alternatives to get this done, but I thought I'll try with the inline for loop.

Edit: The answers are interesting. What is the simplest of way of replacing a list of characters to something else. Same example mentioned below. I have a list in replaceStrings. They must be removed/replaced with Empty/x from the source string s.

s = '£1,000.90'
replaceStrings = ['£', ',']
5
  • You can't do this with an iterator. That returns a separate result for each element of replaceStrings, it doesn't combine them. Use an ordinary loop. Commented Dec 11, 2020 at 22:08
  • 1
    Use re.sub() if you want to replace multiple strings with the same thing. Commented Dec 11, 2020 at 22:08
  • 1
    Welcome to SO! Note that in Python, the "inline for loop" is called a generator expression (python.org/dev/peps/pep-0289). Commented Dec 11, 2020 at 22:09
  • @Barmar I'd say this is a working solution with an iterator: *_, s = ((s := s.replace(x, '')) for x in replaceStrings) Commented Dec 11, 2020 at 22:18
  • @superbrain Can you give a more detailed explanation of this line? It includes a lot of features I have not learned yet. Commented Apr 7, 2022 at 8:10

5 Answers 5

1

(s.replace(x, '') for x in replaceStrings) creates a generator which yields versions of s with strings from replaceStrings removed. Clearly this isn't what you want.

This is a perfect example of when regex would be a good option:

import re

s = '£1,000.90'
s = re.sub('£|,', '', s)
print(s)
Sign up to request clarification or add additional context in comments.

Comments

1

You could also use translate.

>>> s.translate(dict.fromkeys(map(ord, replaceStrings)))
'1000.90'

Or if replaceStrings were a string ('£,'):

>>> s.translate(str.maketrans('', '', replaceStrings))
'1000.90'

But the best option might be the loop:

for c in replaceStrings:
    s = s.replace(c, '')

Btw, replaceStrings seems like a bad name for a "list of characters" that you want to remove. Also, a list of characters is a string thing in Python. Could just use a string then.

Comments

0
"".join([i for i in s if i not in(",","£")])

Hope this aswers your question

Comments

0

Use re.sub like so:

import re
s = '£1,000,000.90'
s = re.sub(r'[£,]', '', s)
print(s)
# 1000000.90

Comments

0

The syntax you're using creates a generator which, when iterated over, will do the replacements. But then, the replacement don't affect s.

The comment suggesting you use an ordinary for loop is correct. You can write that in one line if you insist:

for x in replaceStrings: s = s.replace(x, '')

If you really want to abuse the generator expression, and are using Python 3.8 or later where the walrus operator := is supported, you can also do it like this:

all((s := s.replace(x, '')) for x in replaceStrings)

Here all is the built-in function that returns True if all items in an iterable object are truthy. It exits and returns False if it sees a falsy item. This does have the minor advantage that it will stop if s ever becomes empty, which would mean all other replace operations are unnecessary. Normally, though, this kind of tomfoolery would be a code smell, and your use case doesn't need this behavior.

2 Comments

They're saving all the replacements in s.
No, they're storing a generator in s. They never iterate over it, so none of the replacements are made.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.