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?<
Try using a list comprehension and the ternary operator.
>>> a=[1,2,3,1,3,2,1,1] >>> [4 if x==1 else x for x in a] [4, 2, 3, 4, 3, 2, 4, 4]