user6137931 user6137931 - 1 year ago 56
Python Question

How to randomly replace a string in a list

If I have something like this:

L = ['-','-','-','-','-','-','-']

And let's say that I want to replace certain number of those strings. How do I randomly select a position within the list to replace it for something else? For example:

L = ['-','*','-','-','-','*','*']

Answer Source

Use random.randrange

    import random


for i in range(n):