I have a list with a bunch of words:
lista = ['Jeux Olympiques De Rio\xa02016', 'Sahara Ray', 'Michael Phelps', 'Amber Alert']
I tried to replace the '\xa0':
for element in listor:
element = element.replace('\xa0',' ')
But it didn't work. Also, when I print the elements, it prints:
print(lista[0])
Jeux Olympiques De Rio 2016
Does anyone have an idea on how to solve this?
the easiest way:
lista = [el.replace('\xa0',' ') for el in lista]
for index, element in enumerate(listor):
listor[index] = element.replace('\xa0',' ')
Now you're replacing the string within the list rather than trying to change the string itself. Previously, you were assigning a new string to the same variable name, which doesn't change the previous string but just changes the string that the "element" variable is pointing to. With this, you'll be actually overwriting the list element with a new string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With