In Python it is not possible to modify a list while iterating over it. For example, below I cannot modify list_1 and the result of the print will be [0, 1, 2, 3, 4]. In the second case, I loop over a list of class instances, and calling the set_n method modifies the instances in the list, while iterating over it. Indeed, the print will give [4, 4, 4, 4, 4].
How are these two cases different, and why?
# First case: modify list of integers 
list_1 = [0, 1, 2, 3, 4]
for l in list_1:
    l += 1
print(list_1)
# Second case: modify list of class instances
class Foo:
    def __init__(self, n):
        self.n = n
        
    def set_n(self, n):
        self.n = n
        
list_2 = [Foo(3)] * 5
for l in list_2:
    l.set_n(4)
print([l.n for l in list_2])
 
     
    