-1

Today I find an interesting fact, that I can't re-assign variable in a 'for loop', but why?

Here is my code:

>>> my_list = [1,2,3,4,5]
>>> for i in my_list:
>>>     if i > 3:
>>>        i = 'my_value'
>>> my_list
[1, 2, 3, 4, 5]
Zen
  • 4,381
  • 5
  • 29
  • 56
  • 1
    What did you hope that ``i = "foo"`` would accomplish here? Terminate the loop? When the for loop continues it's next iteration (*NB: Iterators*) ``i`` will point to a new reference object of the next element in the iterator ``[1, 2, 3, 4]``. – James Mills May 14 '14 at 06:36

2 Answers2

5

When you loop over a list with for i in my_list, i isn't bound to a list cell on each iteration. It's bound to the object the list cell referred to. Assigning a new value to i binds i to a new object, but there's no link between i and the list that would cause the list cell to be affected.

An equivalent way to loop over a list would be

for index in range(len(my_list)):
    i = my_list[index]
    whatever_loop_body()

Hopefully, it's clearer in this formulation that i is not an alias for the list cell. (Don't actually loop over lists like this.)

user2357112
  • 260,549
  • 28
  • 431
  • 505
0

Let me guess, you want to have a result like the following

[1, 2, 3, 'my_value', 'my_value']

To do this, you need to do this:

['my value' if i > 3 else i for i in x]

Update

Why your code doesn't work? Because i is not a reference to an object but a primitive value assignment. Python variable reference assignment.

Update 2

If the element of the list is an object rather than a primitive, then it seems that i become a reference to that object.

class A:
    def __init__(self, val):
        self.value = val

my_list = [A(1), A(2),A(3),A(4),A(5)]
for i in my_list:
   if i.value > 3:
       i.value = 'my_value'

print([i.value for i in my_list])
# [1, 2, 3, 'my_value', 'my_value']

Update 3

Please see some of the discussion in the comment below. In python, everything is an object. No primitive.

Ref: For Syntax

Community
  • 1
  • 1
Yeo
  • 11,416
  • 6
  • 63
  • 90