weird iteration/assignment problem
Matimus
mccredie at gmail.com
Fri Jun 13 12:55:04 EDT 2008
On Jun 13, 8:07 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> cirfu schrieb:
>
> > for i in xrange(0, len(texts)):
> > texts[i] = "yes"
>
> > for i in texts:
> > i = "no"
>
> > why is the first one working but not the second. i mean i see why the
> > firts one works but i dont udnerstand why the second doesnt.
>
> Because in the second you only bind the contents of texts to a name i.
>
> But that doesn't mean that i magically became an "alias" for
> texts[index] - it just happens to point at the same object.
>
> To accomplish what you want, the pythonic idiom is to use enumerate:
>
> for i, text in enumerate(texts):
> text[i] = "yes"
>
> Diez
That should be:
for i, text in enumerate(texts):
texts[i] = "yes"
More information about the Python-list
mailing list