6

This is just a trivial question of what convention you suggest. Recently, I have seen many examples of people writing dict(key1=val1, key2=val2) instead of what I think is the more idiomatic {"key1": val1, "key2": val2}. I think the reason is to avoid using "" for the keys, but I am not sure. Perhaps the dict()-syntax looks closer to other languages?

jensgram
  • 31,109
  • 6
  • 81
  • 98
Gurgeh
  • 2,130
  • 15
  • 28
  • 1
    ain't python great .. there's more than one obvious way to do it – wim Jun 14 '12 at 10:33
  • 1
    An obvious advantage of the `{}` version is that if you later decide to use some non-string keys you don't have to rewrite the whole line. – James Jun 14 '12 at 10:38
  • To be on the safe side you could always combine the two - a sort of 'belt and braces' approach: ``kwargs={"key1": "val1", "key2": "val2"}; d = dict(**kwargs)``. – Blair Jun 14 '12 at 10:39
  • 7
    @Blair: that's not safe, that's just wasteful. – Fred Foo Jun 14 '12 at 10:40
  • 3
    @larsmans - its safe in the sense you are guaranteed to have used the idiomatic version ;). That suggestion was made with tongue firmly in cheek. – Blair Jun 14 '12 at 10:44
  • 1
    @wim That contradict the Zen of Python [There should be one-- and preferably only one --obvious way to do it.](http://www.python.org/dev/peps/pep-0020/) – Kien Truong Jun 14 '12 at 10:57
  • @Dikei So the python creators have violated the Zen of Python by making `dict` accept keyword arguments? – Lauritz V. Thaulow Jun 14 '12 at 11:03
  • Well, the Zen of Python is a guideline, not a requirement :) – Kien Truong Jun 14 '12 at 11:25
  • take a look at : http://stackoverflow.com/questions/6388187/what-is-the-proper-way-to-format-a-multi-line-dict-in-python/6389593#6389593 – dugres Jun 14 '12 at 11:32
  • @Blair: ok, time to exercise my irony detection lobe again. – Fred Foo Jun 14 '12 at 12:32

4 Answers4

8

{"key1": val1, "key2": val2} is more idiomatic; I hardly ever encounter dict with keyword arguments and I've certainly never been tempted to write it. It's also more general, because keyword arguments have to be Python identifiers:

>>> {"foo bar": 1}
{'foo bar': 1}
>>> dict(foo bar=1)
------------------------------------------------------------
   File "<ipython console>", line 1
     dict(foo bar=1)
                ^
SyntaxError: invalid syntax

>>> dict("foo bar"=1)
------------------------------------------------------------
   File "<ipython console>", line 1
SyntaxError: keyword can't be an expression (<ipython console>, line 1)
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 1
    There are cases when Python uses words where other languages use punctuations. It produces less line noise. `dict()` might be more readable when it can be used (Obviously it can't be used if keys are not valid Python identifiers; in that case there is no question what to use). – jfs Jun 14 '12 at 10:49
5

I'm going to go against the flow here:

Use the dict() method if it suits you, but keep the limitations outlined in other answers in mind. There are some advantages to this method:

  • It looks less cluttered in some circumstances
  • It's 3 characters less per key-value pair
  • On keyboard layouts where { and } are awkward to type (AltGr-7 and AltGr-0 here) it's faster to type out
Lauritz V. Thaulow
  • 49,139
  • 12
  • 73
  • 92
  • There is obviously not a correct answer here, but I like yours best. I think SO has an unfortunate tendency to automatically vote up comments by users with the highest reputation. – Gurgeh Jun 15 '12 at 08:27
2

Definitely, use {}, keep code simple & type less is always my target

zinking
  • 5,561
  • 5
  • 49
  • 81
  • 4
    Well, here you can't have both. For two or more keys, the `dict()` method will mean less typing: two quotes and one space character less per key-value pair, if you follow PEP8 recommendations. – Lauritz V. Thaulow Jun 14 '12 at 10:49
1

There is some ambiguity with the braces { }. Consider:

>>> x = {'one','two','three'}
>>> type(x)
<type 'set'>
>>> x = {}
>>> type(x)
<type 'dict'>

Whereas there is no ambiguity with using set() or dict().

cdarke
  • 42,728
  • 8
  • 80
  • 84