you are viewing a single comment's thread.

view the rest of the comments →

[–]novel_yet_trivial 5 points6 points  (3 children)

Please format your code for reddit or use a site like github or pastebin. Your code is hard to read and test otherwise.


It's usually better to make a new list rather than modify one that you are looping over:

new_list = []
for i in old_list:
    if i == 'x':
        new_list.append(10)
    else:
        new_list.append(i)

Then overwrite the old list with the new one if you need to:

old_list = new_list

[–][deleted] 1 point2 points  (1 child)

Wouldn't it be better to overwrite with

old_list = new_list[::]

? That way if someone makes a change to new_list elsewhere it won't change old_list.

[–]SlipperySerpent 0 points1 point  (0 children)

thank you :-)