6
5

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.

Pythonのコードを短く簡潔に書くテクニックAdvent Calendar 2017

Day 8

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

Last updated at Posted at 2017-12-07

この記事は 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 ドキュメント
6
5
1

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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?