[issue2793] Dictionary fails to index when adding list when in a deeply nested loop

Amaury Forgeot d'Arc report at bugs.python.org
Thu May 8 22:38:26 CEST 2008


Amaury Forgeot d'Arc <amauryfa at gmail.com> added the comment:

No, there is no problem with Python here.

In your "failing" example, you insert the same object ("new_record")
into the dictionary. Right, you modify its content, but it is still the
same list object; All values in the dictionary are the same object, and
are identical... A list object is said "mutable": if you modify it, you
modify all other references to the list.
A solution is to *copy* the list before adding it into the dictionary,
so that the initial list can be modified without impacting the previous
entries.

By the way, there are at least two more inconsistencies in your example:

1)  record!=''or'\n' 
does not mean what I think you intended. Python reads this as 
     (record != '')    or     ('\n')
Since the right-hand side is a non-empty string, python will evaluate
the condition as True value, always. 
I think you meant something like:
    record != '' or record != '\n' 
which can also be written:
    record not in ('', '\n')

2)   new_record !='\n'
always succeed, since new_record is a list...

----------
nosy: +amaury.forgeotdarc
resolution:  -> invalid
status: open -> closed

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2793>
__________________________________


More information about the Python-bugs-list mailing list