0
1

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】for文で辞書(dictionary)繰り返し処理

Last updated at Posted at 2021-10-22

辞書また、辞書型変数(dictionary)はkey-valueで格納されるデータです。

自分の定義した辞書

myDict = {'John' : 180, 'Naomi': 156, 'Terry': 177, 'Stella': 160}

1. キー(key)だけ取る

1.1 デフォルトでアクセス

for key in myDict:
    print(key)
"""
結果:
John
Naomi
Terry
Stella
"""

1.2 dictionary.keys()でアクセス

for key in myDict.keys():
    print(key)
"""
結果:
John
Naomi
Terry
Stella
"""

2. 値(value)だけ取る

for value in myDict.values():
    print(value)
"""
結果:
180
156
177
160
"""

3. キー、値(key, value)両方とも取る

3.1 デフォルトでアクセス(1.1と一緒)

for key in myDict:
    print(key, myDict[key])
"""
結果:
John 180
Naomi 156
Terry 177
Stella 160
"""

3.2 dictionary.items()でアクセス

for key, value in myDict.items():
    print(key, value)
"""
結果:
John 180
Naomi 156
Terry 177
Stella 160
"""

以上、簡単にメモしました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?