vikky 2405 vikky 2405 - 1 month ago 6
Python Question

How to replace a dot with a string in a python list

my_list = ['b','.','.']

expected_list = ['b','.','w']


May be simple, moving into python recently so any suggestions would be fine

Answer

If you are replacing every occurrence of '.' with 'w' then I would just suggest:

for n, i in enumerate(my_list):
    if i == '.':
        my_list[n] = 'w'

Here is the documentation for how to use the enumerate() function: https://docs.python.org/2/library/functions.html#enumerate