+ 2
What's the difference between creating dictionary with dict() and {}?
It's only the matter of speed?
2 Réponses
+ 3
One difference that comes to my mind is that dict() can be used in variable assignments to create a copy of a dictionary instead of a reference.
For example:
x = {1: 2, 3: 4, 5: 6}
y = x
z = dict(x)
x[1] = 8
print (x[1], y[1], z[1])
>>> 8 8 2
y was changed along with x, as both variables refer to the *same* dictionary, while z points to a *new* dictionary, which is a copy of x. The change in x does not influence z.
You couldn't have done this by using z = {x}, so there - that's the difference ;)
+ 3
There seems to be some differences... look at:
http://stackoverflow.com/questions/6610606/is-there-a-difference-between-using-a-dict-literal-and-a-dict-constructor
( few differences, but too much hard to summarize :P )