I’d like to know if one can write the following statement in one line:
new = ''
for char in text:
if char in blacklist:
new += ' '
else:
new += char
I tried but I get syntax error:
new = ''.join(c for c in text if c not in blacklist else ' ')
I know is not better or prettier, I just want to know if it’s possible.
You’re using your in-line conditional in the wrong place (it’d work if you didn’t have the
else ' 'there, as then it’d just be a filter on the iterable). As it is, you’ll want to do it this way:You could also do it like this if you wanted: