0
0

More than 3 years have passed since last update.

辞書のitemsメソッド

Last updated at Posted at 2020-02-12
items.py
num = {'x':100, 'y':200}
print(num.items())

#辞書をfor文で処理するとキーが返ってくる
for k in num:
    print(k)

#値が欲しい場合は、valuesメソッドを使う
for v in num.values():
    print(v)

#キーと値のペアが欲しい場合は、itemsメソッドを使う
#for文で処理するとk,vにそれぞれキーと値が入る(タプルのアンパッキング)
for k, v in num.items():
    print(k, ':', v)

出力結果:

dict_items([('x', 100), ('y', 200)])
x
y
100
200
x : 100
y : 200
0
0
0

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
0
0