Looking at the C source for the Python dict
operations, you can see that they do a pretty naive (but efficient) copy. It essentially boils down to a call to PyDict_Merge
:
PyDict_Merge(PyObject *a, PyObject *b, int override)
This does the quick checks for things like if they’re the same object and if they’ve got objects in them. After that it does a generous one-time resize/alloc to the target dict and then copies the elements one by one. I don’t see you getting much faster than the built-in copy()
.