How to deep copy a dictionary without .deepcopy

1k Views Asked by At

Is there a way to create a deep copy of a dictionary without the copy module? I need to copy a dictionary of dictionaries N times, but am working in an environment with pared down Jython libraries that do not include copy. I cannot add modules to this system.

I could convert the dictionaries to lists and use [:] to copy, but was wondering if it is possible with dictionaries.

1

There are 1 best solutions below

0
On

So the dictionary-inside-dictionary goes only one level deep? If so, you could try:

copy = {}
for k, v in original.items():
    copy[k] = dict(v)