Let's say I have this list:
lst = [1, 2, 3, 4]
and I want to check if a certain value answers to a condition, and if yes, modify that value. What is the best way to do it? Like a mix of clarity and efficiency. I came up with these 3 options:
# option 1
for i, item in enumerate(lst):
if item == 2:
lst[i] = 7
# option 2
counter = 0
for i in lst:
if i == 2:
lst[counter] = 7
counter += 1
# option 3
for i in range(len(lst)):
if lst[i] == 2:
lst[i] = 7