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?<
Here's a cool and scalable design pattern that runs in O(n) time ...
O(n)
a = [1,2,3,4,5,6,7,6,5,4,3,2,1] replacements = { 1: 10, 2: 20, 3: 30, } a = [replacements.get(x, x) for x in a] print(a) # Returns [10, 20, 30, 4, 5, 6, 7, 6, 5, 4, 30, 20, 10]