I have to search through a list and replace all occurrences of one element with another. So far my attempts in code are getting me nowhere, what is the best way to do this?<
>>> a=[1,2,3,4,5,1,2,3,4,5,1] >>> item_to_replace = 1 >>> replacement_value = 6 >>> indices_to_replace = [i for i,x in enumerate(a) if x==item_to_replace] >>> indices_to_replace [0, 5, 10] >>> for i in indices_to_replace: ... a[i] = replacement_value ... >>> a [6, 2, 3, 4, 5, 6, 2, 3, 4, 5, 6] >>>