Numpy problem: Arrays in a list of dictionaries

Robert Kern robert.kern at
Tue Apr 24 01:14:44 CEST 2007

ZMY wrote:
> Dear Robert,
> So how should I do this? I tried
>>>>> d1 = {1: array([2, 3, 4]).copy(), 2: ''}
>>>>> l1 = []
>>>>> for i in range(3): l1.append(d1.copy())
> and it still doesn't work.

Of course it doesn't, for the same reason that your first attempt didn't work
either. You've made a copy of an array and put the copy into the dictionary,
then made copies of that dictionary. The process of making copies of the
dictionary doesn't make copies of the objects it contains.

> I think my problem is: how to create a new array every time I append
> it to the list...

import copy

d1 = {1: array([2, 3, 4]), 2: ''}
l1 = []
for i in range(3):

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

More information about the Python-list mailing list