# 辞書オブジェクトのforループ ###### tags: `Python` 辞書型dictのオブジェクトをforループで回すと、辞書のキーが取得できる。 ```python= d = {'key1': 'Python', 'key2': 'PHP', 'key3': 'JAVA'} for i in d: print(i) ``` **Results:** ``` key1 key2 key3 ``` ## keys(): 各要素のキーに対してforループ処理 辞書オブジェクトをそのままfor文で回してもキーkeyが取得できるが、keys()メソッドを使ってもよい。 ```python= d = {'key1': 'Python', 'key2': 'PHP', 'key3': 'JAVA'} for i in d.keys(): print(i) ``` **Results:** ``` key1 key2 key3 ``` ## values(): 各要素の値に対してforループ処理 ```python= d = {'key1': 'Python', 'key2': 'PHP', 'key3': 'JAVA'} for i in d.values(): print(i) ``` **Results:** ``` Python PHP JAVA ``` ## items(): 各要素のキーと値に対してforループ処理 ```python= d = {'key1': 'Python', 'key2': 'PHP', 'key3': 'JAVA'} for i in d.items(): print(i) ``` **Results:** ``` ('key1', 'Python') ('key2', 'PHP') ('key3', 'JAVA') ``` (key, value)のタプルとして受け取ることもできる。 ```python= d = {'key1': 'Python', 'key2': 'PHP', 'key3': 'JAVA'} for i in d.items(): print('key = ', i[0]) print('value = ', i[1]) ``` **Results:** ``` key = key1 value = Python key = key2 value = PHP key = key3 value = JAVA ``` ```python= d = {'key1': 'Python', 'key2': 'PHP', 'key3': 'JAVA'} for k, v in d.items(): print('key = ', k) print('value = ', v) ``` **Results:** ``` key = key1 value = Python key = key2 value = PHP key = key3 value = JAVA ``` https://ja.wikipedia.org/wiki/%E3%83%87%E3%83%BC%E3%82%BF%E6%A7%8B%E9%80%A0 http://jsonviewer.stack.hu/ https://openweathermap.org/api/one-call-api https://opentdb.com/api_config.php https://developer.musixmatch.com/documentation/api-reference/artist-get