The problem is here:
str(i)+str(j)
This does not produce unique identifiers. For example, the value set when i=1
and j=11
will be overwritten by the value set when i=11
and j=1
(there are many more instances as well).
You can fix the problem by inserting some delimiter character between the two numbers (such as an underscore like you have between j
and k
).