What is the best way to make a shallow copy of a Python dictionary?

Suppose we have a simple Python dictionary:

dict_ = {'foo': 1, 'bar': 2} 

What is the best way to copy this dictionary?

 copy1 = dict(dict_) copy2 = dict_.copy() 

Is there a good reason to support one approach over another?

+7
source share
6 answers

You can use option # 1 ( dict(dict_) ) because of the principle of least surprise: you can do the same for lists: list(list_) , and the copy() method does not exist in lists.

However, the responses raised many good points. This means that there can be no clearly better solution , and both of them are great if you do what you intend to do (the point regarding the dict subclass can be crucial, in some codes, for example). So, I would choose what seems right for your application, given the points raised in the answers!

+3
source

I always use the dict constructor: it makes it obvious that you are creating a new dict , while calling the copy method of an object can copy anything. Similarly, for list I prefer to call the constructor over copy by slicing.

Note that if you use dict subclasses with the copy method, you may be confused:

 >>> from collections import defaultdict >>> d = defaultdict(int) >>> d['a'] 0 >>> d.copy() defaultdict(<class 'int'>, {'a': 0}) >>> dict(d) {'a': 0} >>> 

The copy defaultdict method provides you with another defaultdict , but if you do not override copy in a subclass, the default action is simply to specify a dict :

 >>> class MyDict(dict): pass >>> d = MyDict(a=1) >>> d {'a': 1} >>> type(d) <class '__main__.MyDict'> >>> type(d.copy()) <class 'dict'> 

This means that you need to know about the inner details of the subclass to find out what type the copy method returns.

+8
source

It really depends ... on what you are actually going to use it for. In the library, you may need to modify a copy of some dictated object, and you want the copy to be the same object. Other APIs must have an actual dict , so create a new dict using the constructor method and use it. But when you do this, you lose information about the original type (and methods). But in general, I would prefer the copy () method, since it is clear that this is a copy of the dictaphone object (and not just the dict). This makes it more flexible (duck print).

+3
source

Best would be dict_.copy() , as the intention is self-describing. However, using the dict form, you can create a copy with additional keys:

 d = dict(a, zoo=1, zar=2) 

which is equivalent but shorter:

 d = a.copy() d.update(zoo=1, zar=2) 
+2
source

The second is better because it explicitly shows that you are copy 'ing.

+1
source

I don't like the dict.copy() method:

  • its incomprehensible on behalf of which copy of .copy() does
  • subclasses must overwrite it, or its semantics are completely closed
  • small copies can be done in other ways (general copy.copy() or dict() )

Imagine a random class, for example. Uber . - How do you try to make a copy of u = Uber(...) ?

  • dict(u)
  • Uber(u)
  • u.copy()
  • copy.copy(u)

I would say that number 2. has the greatest chance of working or blowing up. “Exactly what I want.”

0
source

All Articles