3

Consider these two statements, which serve the same purpose:

tel = {'sape': 4139, 'jack': 4098}

and

tel = dict([('sape', 4139), ('jack', 4098)])

Why use "dict()" at all?

I am sure there is a reason, i just want to know it.

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
mathgenius
  • 503
  • 1
  • 6
  • 21
  • 2
    I think [this](http://stackoverflow.com/questions/6610606/dict-literal-vs-dict-constructor-any-preferred) QA will answer your question. Basically the `dict()` call is more flexible and can do a few things that the `{}` can't do. Like use kwargs to create a dict, create a dict from a list directly, etc. – KobeJohn Mar 26 '15 at 13:16

2 Answers2

4

The reason for the existence of dict(...) is that all classes need to have a constructor. Furthermore, it may be helpful if the constructor is able to take in data in a different format.

In your example use case, there is no benefit in using dict, because you can control the format the data is in. But consider if you have the data already as pairs in a list, the dict constructor may be useful. This can happen e.g. when reading lines from a file.

juhist
  • 4,210
  • 16
  • 33
2
map(dict,[[(1,2)]])
[{1: 2}]

map({},[[(1,2)]])
TypeError: 'dict' object is not callable
enthus1ast
  • 2,099
  • 15
  • 22