If I have a list of numbers, and I want to increment them using a for loop, why isn\'t this working:
>>> list=[1,2,3,4,5]
>>> for num in l
This is because you only get a reference to the variable in the list. When you replace it, you simply change what that reference is pointing to, so the item in the list is left unchanged. The best explanation for this is to follow it visually - step through the execution visualization found at that link and it should be very clear.
Note that mutable objects can be mutated in place, and that will affect the list (as the objects themselves will have changed, not just what the name you are working with is pointing to). For comparison, see the visualization here.
The best option here is a list comprehension.
new_list = [num + 1 for num in old_list]