I have a list of elements ("tokens") that I assume are acting like strings. I want to find words with an underscore in them and replace the underscore with a space. I have the following code:
for e in tokens:
if '_' in e:
cmpd = list(e)
cmpd[e.find('_')] = ' '
''.join(cmpd)
new_tokens[index] = cmpd
It's basically identical to what's here: Change one character in a string in Python?
Later I'm trying to concatenate all the list elements in a sentence, each separated by a space, but I get the following error:
TypeError: can only concatenate list (not "str") to list
And if I print out e
and cmpd
, I get this output:
e: my_string
cmpd: ['m', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g']
Why is cmpd a list and not a string?