60

I like to know, which is the best practice to declare dictionary in below 2 approaches and why?

>>>a=dict(one=2, two=3)  # {"two":3, "one":2}
>>>a={"two":3, "one":2}
Cœur
  • 37,241
  • 25
  • 195
  • 267
user1559873
  • 6,650
  • 8
  • 25
  • 28
  • 2
    FWIW, there is one difference I have found. dict(zip(foo)) is possible. Not sure why {zip(foo)} is not possible in either python 2 or 3. I came here hoping to learn why. – netskink Aug 01 '18 at 20:22
  • 1
    Why is this question closed? People who voted to close it rather could have spent effort to improve it by adding criteria to define `better` instead of closing. – mahoriR Aug 25 '20 at 10:50
  • 1
    "Which is better" without defining what "better" means is way to broad – Nico Haase Aug 25 '20 at 12:19

3 Answers3

48

Would you believe someone has already analyzed that (from a performance perspective).

With CPython 2.7, using dict() to create dictionaries takes up to 6 times longer and involves more memory allocation operations than the literal syntax. Use {} to create dictionaries, especially if you are pre-populating them, unless the literal syntax does not work for your case.

Fuyu Persimmon
  • 483
  • 6
  • 13
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
22

The second one is clearer, easier to read and it's a good thing that a specific syntax exists for this, because it's a very common operation:

a = {"two":3, "one":2}

And it should be preferred on the general case. The performance argument is a secondary concern, but even so, the {} syntax is faster.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
15

In Python you should always use literal syntax whenever possible. So [] for lists, {} for dicts, etc. It's easier for others to read, looks nicer, and the interpreter will convert it into bytecode that is executed faster (special opcodes for the containers, instead of performing function calls).

Anorov
  • 1,990
  • 13
  • 19
  • 8
    I don't find subjective reasons like "easier for others to read, looks nicer" good arguments :) – Matt Jan 06 '20 at 17:49
  • 1
    @Matt I think the main take away is " into bytecode that is executed faster", and others are subjective to strengthen the argument, but to echo your sentiment: `{[(name, cls) for name, cls in dataset_types.__dict__.items() if isinstance(cls, type)]}` vs `dict([(name, cls) for name, cls in dataset_types.__dict__.items() if isinstance(cls, type)])`, I think the version with `dict()` is much easier to read. – QuirkyBit Dec 04 '20 at 18:24
  • 1
    @Matt I think they are. And as stated in the other comment, there are instances where the non-literal syntax is easier to read. I think it's best to use whatever is easiest to read in each circumstance. But, yes, the literal syntax also has less performance overhead. – Anorov Mar 25 '21 at 19:18
  • 4
    `dict` is "easier to read" than `{}` and the meaning is clearer... especially for those who use multiple languages and find it difficult to remember whether it's braces, brackets, curly braces... – PatrickT Jul 24 '21 at 23:45
  • 1
    @Matt - it's actually not subjective. Readability counts is PEP20. – jhnwsk Nov 02 '21 at 08:00
  • @jhnwsk - I find `dict(foo=bar)` easier to read and looks more like python to me. – Matt Nov 12 '21 at 15:42
  • Me too @Matt, definitely points for dict() for readability and {} for speed. So there is no clear "better". I usually go for dict and optimize if necessary. – jhnwsk Nov 13 '21 at 17:32
  • If I pulled someone off the street and asked which creates a list `[]` or `list()`, which do you think most people would guess? – joeyagreco Jul 27 '22 at 15:53