Why can't you assign to a list in a loop without enumerate?

Bruno Desthuilliers onurb at xiludom.gro
Tue Oct 31 20:36:16 CET 2006

```Danny Colligan wrote:
> In the following code snippet, I attempt to assign 10 to every index in
> the list a and fail because when I try to assign number to 10, number
> is a deep copy of the ith index (is this statement correct?).

It's quite easy to find out:

class Foo(object):
def __init__(self, num):
self.num = num
def __repr__(self):
return "foo %d at %d" % (self.num, id(self))

foos = [Foo(i) for i in range(10)]
print foos
=> [foo 0 at 47281508865040, foo 1 at 47281508865104, foo 2 at
47281508865168, foo 3 at 47281508865232, foo 4 at 47281508865296, foo 5
at 47281508865360, foo 6 at 47281508865424, foo 7 at 47281508865488, foo
8 at 47281508865552, foo 9 at 47281508865616]

for foo in foos:
foo.num = foo.num * 2
print foos
=> [foo 0 at 47281508865040, foo 2 at 47281508865104, foo 4 at
47281508865168, foo 6 at 47281508865232, foo 8 at 47281508865296, foo 10
at 47281508865360, foo 12 at 47281508865424, foo 14 at 47281508865488,
foo 16 at 47281508865552, foo 18 at 47281508865616]

Seems like your statement is *not* correct.

>>>> a = [1,2,3,4,5]
>>>> for number in a:
> ...     number = 10

Note that you are *not* "assign(ing) 10 to every index in the list"
here. Rebinding the local name 'number' in each iteration only makes
this name refer to another object (implying of course loosing the
reference to the current list element). Then - on the following
iteration - the name 'number' is rebound to the next object in the list.

>>>> a
> [1, 2, 3, 4, 5]

>>> for foo in foos:
...     original = foo
...     foo = Foo(10)
...     print "original : %s - foo : %s" % (original, foo)
...
original : foo 0 at 47281508865040 - foo : foo 10 at 47281508864144
original : foo 2 at 47281508865104 - foo : foo 10 at 47281508864144
original : foo 4 at 47281508865168 - foo : foo 10 at 47281508864144
original : foo 6 at 47281508865232 - foo : foo 10 at 47281508864144
original : foo 8 at 47281508865296 - foo : foo 10 at 47281508864144
original : foo 10 at 47281508865360 - foo : foo 10 at 47281508864144
original : foo 12 at 47281508865424 - foo : foo 10 at 47281508864144
original : foo 14 at 47281508865488 - foo : foo 10 at 47281508864144
original : foo 16 at 47281508865552 - foo : foo 10 at 47281508864144
original : foo 18 at 47281508865616 - foo : foo 10 at 47281508864144

> So, I have to resort to using enumerate to assign to the list:

Yes. That's still far better than having to manually check for sequence
boundaries and track current index.

>>>> for i, number in enumerate(a):
> ...     a[i] = 10
> ...
>>>> a
> [10, 10, 10, 10, 10]

Just for the record: the notation 'a[x] = y' in fact calls
'a.__setitem__(x, y)'. It's really just calling a method that alters the
state of object a.

> My question is, what was the motivation for returning a deep copy of
> the value at the ith index inside a for loop instead of the value
> itself?

Forget about values. Think objects.
>>> i = 10
>>> i.__class__
<type 'int'>
>>> dir(i)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
'__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
'__floordiv__', '__getattribute__', '__getnewargs__', '__hash__',
'__hex__', '__init__', '__int__', '__invert__', '__long__',
'__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__',
'__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__',
'__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__',
'__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__',
'__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__',
'__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']
>>> i.__abs__()
10

> Also, is there any way to assign to a list in a for loop (with
> as little code as used above) without using enumerate?

What's the problem with enumerate() ?

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"

```