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

D

Danny Colligan

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?).
a = [1,2,3,4,5]
for number in a:
.... number = 10
....[1, 2, 3, 4, 5]

So, I have to resort to using enumerate to assign to the list:
.... a = 10
....[10, 10, 10, 10, 10]

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? Also, is there any way to assign to a list in a for loop (with
as little code as used above) without using enumerate?

Thanks,

Danny
 
F

Fredrik Lundh

Danny said:
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?

I'm not sure the words "deep copy" and "value" really means what you
think they do. maybe you should spend a little time with Python's
documentation, instead of making up absurd theories about how things
might work, only to get disappointed every time reality disagrees.
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 wrong with using enumerate? or a list comprehension? or some
other of the many different ways you can use to build a list from a set
of values?

</F>
 
D

Duncan Booth

Danny Colligan said:
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?).

No. There is no copying involved.

Before the assignment, number is a reference to the object to which the ith
element of the list also refers. After the assignment you have rebound the
variable 'number' so it refers to the value 10. You won't affect the list
that way.
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?

There is no copying going on. It returns the value itself, or at least a
reference to it.
Also, is there any way to assign to a list in a for loop (with
as little code as used above) without using enumerate?

a[:] = [10]*len(a)

or more usually something like:

a = [ fn(v) for v in a ]

for some suitable expression involving the value. N.B. This last form
leaves the original list unchanged: if you really need to mutate it in
place assign to a[:] as in the first example, but if you are changing all
elements in the list then you usually want a new list.
 
D

dakman

I'm not quite sure what your asking, but I'll give it a shot.

You do not have to use enumerate, you can use other methods just as
range(len(sequence)), but the reason you cannot asign a value is
because a for loop iterates a sequence meaning when you do

for a in [1, 2, 3, 4, 5]:
...
a is just a value pulled from the sequence iteration.

just like i and number is a value pulled from the iteration in

for i, number in enumerate(a):
...

(That was worded badly :/ I apologise.)
Danny said:
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?).
a = [1,2,3,4,5]
for number in a:
... number = 10
...[1, 2, 3, 4, 5]

So, I have to resort to using enumerate to assign to the list:
... a = 10
...[10, 10, 10, 10, 10]

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? Also, is there any way to assign to a list in a for loop (with
as little code as used above) without using enumerate?

Thanks,

Danny
 
D

Danny Colligan

I see. Thanks for the helpful response.

Danny

Duncan said:
Danny Colligan said:
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?).

No. There is no copying involved.

Before the assignment, number is a reference to the object to which the ith
element of the list also refers. After the assignment you have rebound the
variable 'number' so it refers to the value 10. You won't affect the list
that way.
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?

There is no copying going on. It returns the value itself, or at least a
reference to it.
Also, is there any way to assign to a list in a for loop (with
as little code as used above) without using enumerate?

a[:] = [10]*len(a)

or more usually something like:

a = [ fn(v) for v in a ]

for some suitable expression involving the value. N.B. This last form
leaves the original list unchanged: if you really need to mutate it in
place assign to a[:] as in the first example, but if you are changing all
elements in the list then you usually want a new list.
 
B

Bruno Desthuilliers

Danny said:
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.
.... 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.
... a = 10
...[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.['__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__']
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() ?
 
T

Tim Chase

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?).

Sorta...just like with function parameters, there are mutables
and immutables.
a = [1,2,3,4,5]
for number in a:
... number = 10
...[1, 2, 3, 4, 5]

Just as with a function (currently discussed on another thread
recently), you have the following behavior:
.... x = 42
....
.... x.append(42)
....
>>> g = 1
>>> h = [1]
>>> a(g)
>>> b(h)
>>> g 1
>>> h
[1, 42]


you have similar behavior:
>>> a = [[1],[2],[3],[4],[5]]
>>> for thing in a:
.... thing.append(10)
....[[1, 10], [2, 10], [3, 10], [4, 10], [5, 10]]

Lists/sets/etc are mutable. Strings, numbers,
So, I have to resort to using enumerate to assign to the list:
... a = 10
...[10, 10, 10, 10, 10]


This would commonly be written with a list comprehension:

a = [10 for _ in a]

or

a = [10] * len(a)

or, if that was a general case of something more specific with
some "if" brains behind it, you can do things like

a = [odd(v) and 10 or v for v in a]

to only change them to 10 where the value is odd.
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? Also, is there any way to assign to a list in a for loop (with
as little code as used above) without using enumerate?


As stated above, it returns the item...if it's mutable, you can
mutate it. If it's an immutable (like your numbers), you just
change the variable in the local scope of the loop.


-tkc
 
D

Dennis Lee Bieber

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?).
No... Mentally unroll your loop...

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

a = [1,2,3,4,5]
number = a[0] #first iteration
number = 10
number = a[1] #second iteration
number = 10
#etc.


Secondly, in Python, scalars, strings, and tuples are "immutable"
and cannot be changed once defined.

Third, names do not define storage spaces (this is different from
practically all other languages, where the variable name defines the
storage space and assignment changes the contents of that space). Names
are movable, and "move" to identify the object. Assignment does not,
normally, copy an object. So... Going further into the loop unrolling...

a[0] "references" the object "1"; after
number = a[0]
number /also/ "references" the object "1"; it has no connection to
the list object "a".
number = 10
rebinds number to "reference" the object "10"; the "reference" to
the object "1" has been tossed; a[0] still has that reference.

For all practical purposes, the only way in Python to modify an
object rather than rebind to a different object requires one to qualify
the destination name:

somename = someobject #rebinds
somename[x] = someobject #modifies (it is rebinding the "x"
component inside of somename)
#this syntax handles lists and
dictionaries
somename.x = someobject #modifies somename by rebinding the "x"
component inside of somename; class, instance, or imported module syntax

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
B

Bjoern Schliessmann

Fredrik said:
what's wrong with using enumerate? or a list comprehension? or
some other of the many different ways you can use to build a list
from a set of values?

Shouldn't there be one -- and preferably only one -- obvious way to
do it? 8)

Python Cookbook says "enumerate()", is this still correct with 2.5?

Regards,


Björn
 
F

Fredrik Lundh

Bjoern said:
Shouldn't there be one -- and preferably only one -- obvious way to
do it? 8)
>
Python Cookbook says "enumerate()", is this still correct with 2.5?

define "it".

</F>
 
R

Roberto Bonvallet

Bjoern said:
Shouldn't there be one -- and preferably only one -- obvious way to
do it? 8)

The obvious one is to use enumerate.
TOOWTDI allows less obvious ways to exist.
 
B

Bjoern Schliessmann

Fredrik said:
define "it".

Sorry! I mean iterating over a list and having the list index
available in the loop. Like:

for i, thing in enumerate(things):
pass

Regards,


Björn
 
F

Fredrik Lundh

Bjoern said:
Sorry! I mean iterating over a list and having the list index
available in the loop. Like:

for i, thing in enumerate(things):
pass

"enumerate" is the obviously right thing for this, sure. but it's not
necessarily the right thing for the OP's "I want to create a new list
based on an existing list". modifying the existing list is usually not
the most efficient way to do that.

</F>
 
B

Bjoern Schliessmann

Fredrik said:
"enumerate" is the obviously right thing for this, sure.
K.

but it's not necessarily the right thing for the OP's "I want to
create a new list based on an existing list". modifying the
existing list is usually not the most efficient way to do that.

Agreed.

Regards,


Björn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top