On 24/03/2016 14:34, Jussi Piitulainen wrote:
BartC writes:
On 24/03/2016 14:08, Jon Ribbens wrote:
On 2016-03-24, BartC wrote:
I'd presumably have to do:

    for i in range(len(L)):
      L[i]=0

That's kind've a weird thing to want to do;

The thing I'm trying to demonstrate is changing an element of a list
that you are traversing in a loop. Not necessarily set all elements to
the same value.

You understand correctly, but it may be more natural in practice to
write it this way:

     for k, item in enumerate(them):
         them[k] = f(item)

I _think_ I might write it that way even when "f(item)" does not depend
on the old value at all, but I don't expect to be in that situation.


Yes, you're right. Usually the update is conditional on the existing value. But my too-simple example would have had an unneeded item variable.

--
Bartc
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to