Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast way to copy dictionary in Python

I have a Python program that works with dictionaries a lot. I have to make copies of dictionaries thousands of times. I need a copy of both the keys and the associated contents. The copy will be edited and must not be linked to the original (e.g. changes in the copy must not affect the original.)

Keys are Strings, Values are Integers (0/1).

I currently use a simple way:

newDict = oldDict.copy() 

Profiling my Code shows that the copy operation takes most of the time.

Are there faster alternatives to the dict.copy() method? What would be fastest?

like image 595
Joern Avatar asked May 02 '11 19:05

Joern


People also ask

Are Python Dicts fast?

As far I have understood, python dict is a quite fast implementation of a hashtable. Is there anything better than that for my specific case? If your using Python 3.5 or lower, then the dictionary built in in Python 3.6 is said to be 20-25% faster than the old dictionary builtin in Python 3.5.

How do I save a large dictionary in Python?

If you just want to work with a larger dictionary than memory can hold, the shelve module is a good quick-and-dirty solution. It acts like an in-memory dict, but stores itself on disk rather than in memory. shelve is based on cPickle, so be sure to set your protocol to anything other than 0.

How do I copy and delete a dictionary?

The dict. clear() to delete a dictionary in Python Python has in-built clear() method to delete a dictionary in Python. The clear() method deletes all the key-value pairs present in the dict and returns an empty dict.


1 Answers

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().

like image 165
Daniel DiPaolo Avatar answered Sep 17 '22 14:09

Daniel DiPaolo