23
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

dictの作成は「{}」より「dict()」のほうが簡潔に書ける

Last updated at Posted at 2017-12-04

この記事は Pythonのコードを短く簡潔に書くテクニック Advent Calendar 2017 の5日目です。

はじめに

Pythonでdictを作成するときは通常{}を使うことが多いですが、キーを''または""で囲う必要があるので若干面倒です。

dict()で書くとキー名を囲う必要がないので若干短く書くことができます。
文字数はそれほど変わらないですが、入力がとても楽になります。

{}で書いた場合

d = {'title': 'title1', 'user': 'user1', 'body': 'body'}

dict()で書いた場合

d = dict(title='title1', user='user1', body='body1')

参考

23
15
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?