For example:
def update_condition(self, type, params):
for condition in self.conditions:
condition_loaded = json.loads(condition)
if condition_loaded['type'] == type:
condition_loaded['params'] = params
condition = json.dumps(condition_loaded)
The above code does nothing because condition isn’t by reference. What’s the proper way to do this?
You could use
enumerate:But, in general, these things are a little cleaner with helper functions and list comprehensions:
It should be noted that this second solution doesn’t update the list in place — In other words, if you have other references to this list, they won’t be influenced. If you want, you can make the substitution in place pretty easily using slice assignment: