0
0

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】メモ 辞書について

Posted at

##Pythonの辞書(dict)のforループ処理(keys, values, items)

Pythonの辞書オブジェクトdictの要素をfor文でループ処理するには辞書オブジェクトdictのメソッドkeys(), values(), items()を使う。

test = {'key1': 1, 'key2': 2, 'key3': 3}

keys(): 各要素のキーに対してforループ処理
※普通にfor文回しても同じのができる

for A in test.keys():
    print(A)

# key1
# key2
# key3

values(): 各要素の値に対してforループ処理

for B in test.values():
    print(B)

# 1
# 2
# 3

items(): 各要素のキーと値に対してforループ処理

for C, D in test.items():
    print(C, D)

# key1 1
# key2 2
# key3 3
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?