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 1 year has passed since last update.

【Python】辞書に含まれるキーと値を取得する方法

Posted at

【Python】辞書に含まれるキーと値を取得する方法について書いていく

1.辞書の「キー」の取得方法(2種類) keys()

①keys()を使わなくてもキーは取れる

drink = {'c': 'coffee', 'o': 'orange', 't': 'tea'}
for a in drink:
    print(a)
実行結果
c
o
t

②keys()を使ったキーの取得

drink = {'c': 'coffee', 'o': 'orange', 't': 'tea'}
for a in drink.keys():                               # drinkの後ろに、keys()を入れるだけの違い
    print(a)
実行結果
c
o
t

2.辞書に含まれる値を取得 values()

drink = {'c': 'coffee', 'o': 'orange', 't': 'tea'}
for a in drink.values():                              
    print(a)
実行結果
coffee
orange
tea

3.キーと値をどちらも取得 items()

drink = {'c': 'coffee', 'o': 'orange', 't': 'tea'}
for b, c in drink.items():
    print(b, c)
実行結果
c coffee
o orange
t tea
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?