python3

python3 の辞書で、キー一覧を取り出して表示する方法

投稿日:2020年12月20日 更新日:

keys()メソッドでは、キー一覧を取得できる。

values() では、値を取得できる。

items() では、キーと値をタプルとして取得できる。

リストに変換すると、print() でコンソールに表示できる。

mydict = {'a':1,'b':2,'c':123}

print(list(mydict.keys()))
print(list(mydict.values()))
print(list(mydict.items()))

結果

['a', 'b', 'c']
[1, 2, 123]
[('a', 1), ('b', 2), ('c', 123)]
[商品価格に関しましては、リンクが作成された時点と現時点で情報が変更されている場合がございます。]

黒子のバスケ カラー版 1【電子書籍】[ 藤巻忠俊 ]
価格:528円(税別、送料別)(2020/12/24時点)

楽天で購入

-python3
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

python3 で、すべて小文字かどうかを判定する方法

islower() 関数を使う。 例 print(‘abcde’.islower()) print(‘abcDe’.islower()) 結果 True False

no image

python でファイルを1行おきに読み込む方法

readline を使って次のように書く。 例 ファイル:data.txt # id name age 1 佐藤太郎 10 2 鈴木花子 18 3 坂本明美 21 4 松村光子 24 5 小川奏子 1 …

no image

matplotlib で、x,y の軸上の数値を表示する方法

ax の xtics と ytics を使って表示する。 詳細は次のドキュメントを参照。 https://matplotlib.org/stable/gallery/lines_bars_and_ma …

no image

python3 で、フォルダがなければ作成するプログラム

os.mkdir() 関数を使う。 すでにフォルダが存在している場合は、エラーを表示する。 例 import os folder_name = ‘./python_create_folder’ try …

no image

python3 で、リストを pop したものの返り値

リストを pop すると、 pop された値が返される。 例 plant_1 = [’pumpkin’, ‘ginger’, ‘potato’] plant_2 = [] print( …