3
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 3 years have passed since last update.

Python、辞書型についてメモ

Last updated at Posted at 2020-03-26

#python辞書型についてメモ

dic = {"みかん":2,"りんご":10,"いちご":2}

keyを取り出す

dic.keys()
dict_keys(['みかん', 'りんご', 'いちご', 'メロン'])

値を取り出す

dic.values()
dict_values([2, 10, 2, 44])

キーと値の両方を取り出す

dic.items()
dict_items([('みかん', 2), ('りんご', 10), ('いちご', 2), ('メロン', 44)])

for文でキーと値の両方を取り出す

for i,k in dic.items():
    print(i,k)
みかん 2
りんご 10
いちご 2
メロン 44

新たなキーと値を追加する。

dic['ぶどう'] = 10
dic.items()
dict_items([('みかん', 2), ('りんご', 10), ('いちご', 2), ('メロン', 44), ('ぶどう', 10)])
3
5
3

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
3
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?