Take the 2-minute tour ×

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?

share|improve this question
If the value can be either 0 or 1, would a bool be a better choice than an int? –  Samir Talwar May 2 '11 at 19:31
And if you need thousands of copies of them, would bitmasks work even better? –  Wooble May 2 '11 at 19:38
@Samir isn't bool in Python named int anyway. –  Santa May 2 '11 at 19:42
I agree, though, that a bitmask might be more efficient for you (depending on how you use this "dict", really). –  Santa May 2 '11 at 19:42
To clarify, the bool type is actually a subclass (subtype?) of the int type. –  Santa May 2 '11 at 20:04

4 Answers 4

up vote 43 down vote accepted

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

share|improve this answer
Sounds like I better rewrite the code to avoid the use of dicts at all - or use a faster data structure that can do the same job. Thank you very much for the answer! –  Joern May 2 '11 at 19:49
+1 right way to answer! –  katrielalex May 2 '11 at 20:32

Appearantly dict.copy is faster, as you say.

[utdmr@utdmr-arch ~]$ python -m timeit -s "d={1:1, 2:2, 3:3}" "new = d.copy()"
1000000 loops, best of 3: 0.238 usec per loop
1000000 loops, best of 3: 0.621 usec per loop
[utdmr@utdmr-arch ~]$ python -m timeit -s "from copy import copy; d={1:1, 2:2, 3:3}" "new = copy(d)"
1000000 loops, best of 3: 1.58 usec per loop
share|improve this answer
Thanks for the comparison! Will try to rewrite the code as to avoid the use of dict copying in most places. Thanks again! –  Joern May 2 '11 at 19:51
The way to do the last comparison without counting the cost of doing the import every time is with timeit's -s argument: python -m timeit -s "from copy import copy" "new = copy({1:1, 2:2, 3:3})". While you're at it, pull out the dict creation as well (for all examples.) –  Thomas Wouters May 2 '11 at 19:56
@Thomas Wouters, thanks, edited answer :). –  utdemir May 2 '11 at 22:02
Maybe repeat the processes many times is better since there might be some fluctuations of one specific shot. –  xiaohan2012 Feb 17 at 18:27
Timeit does that; as it says it loops 1000000 times and averages it. –  utdemir Feb 18 at 12:01

Can you provide a code sample so I can see how you are using copy() and in what context?

You could use

new = dict(old)

But I dont think it will be faster.

share|improve this answer
Thank you for informing me about what constitutes a comment. I can't post "comments" because I dont have enough rep. –  MikeVaughan May 2 '11 at 19:35
Yes. Let me first find help on posting and formatting code. –  Joern May 2 '11 at 19:36

Depending on things you leave to speculation, you may want to wrap the original dictionary and do a sort of copy-on-write.

The "copy" is then a dictionary which looks up stuff in the "parent" dictionary, if it doesn't already contain the key --- but stuffs modifications in itself.

This assumes that you won't be modifying the original and that the extra lookups don't end up costing more.

share|improve this answer

Your Answer


