0
Removing list's elements from string PYTHON
How can I remove all list's elements from string For example: list = ["1", "2", "3"] str = "random1 random2 random3" And I want it to become "random random random"
8 odpowiedzi
+ 4
Матвей Стаселько maybe you can do it this way,
import re
for i in list:
str=re.sub(i,"",str)
print(str)
Just saying that you shouldn't use keywords like list as variables name .
+ 2
Матвей Стаселько u can use
variable.translate()
Example :
list = ['1', '2', '3']
str = "random1 random2 random3"
newStr = str.translate({ord(x): '' for x in list})
print(newStr)
+ 1
Матвей Стаселько i mean should it output "random" for something like "1random" or "ran1dom" ?
+ 1
Abhay,
Wait
I was wrong, i want that result that you wrote
0
And if it was "ran1dom" or "1random" instead of random1 ?
0
Abhay,
Yes
0
Abhay,
No, i just needed to remove all elements from the list