Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
5
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

既存のdictに複数の値を一度に追加したいときはupdate()を使うと1行で書ける

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

はじめに

既存のdictオブジェクトに値を追加したいときにd[key] = valueという書き方をしますが、一度に複数の値を追加したい時はupdate()を使うと1行で書けてスッキリします。

d[key] = valueで追加する場合

d['apple'] = 100
d['banana'] = 200
d['orange'] = 300

update()で追加する場合

d.update(apple=100, banana=200, orange=300)

参考

  • Python3 ドキュメント
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
5
Help us understand the problem. What are the problem?