Replace one string in a list of strings

python

Solution

Your error seems to indicate that your string_list is not a list of string but a real string (that doesn't support assignement because a string is immutable).

If your string_list was a real list of strings, like this for example : `string_list = ["first", "second", "", "fourth"]`, then you will be able to do `string_list[2] = "third"` to obtain `string_list = ["first", "second", "third", "fourth"]`.

If you need to automatically detect where an empty string is located in your list, try with index :

string_list[string_list.index("")] = "replacement string"

print string_list
>>> ["first", "second", "replacement string", "fourth"]

Problem

I have a list of strings. If one of the strings (e.g. at index 5) is the empty string, I want to replace it with "N". How do I do that? The naive method (for a java programmer) does not work: ``` string_list[5] = "N" ``` gives 'str' object does not support item assignment (string_list originally came from a .csv-file- that is why it might contain empty strings.)

Original source

Related problems