12
13

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 5 years have passed since last update.

dictionaryのkeyとvalueを全て取得する方法

Posted at

自分のメモ用に。
しっかり調べてないので、補足やもっと便利なものがあれば教えてくださると助かります!

#keyのリスト [keys()]

dictionary = {'key1': [645, 469], 'key2': "value", 'key3': 3}
print (dictionary.keys())

#valueのリスト [values()]

dictionary = {'key1': [645, 469], 'key2': "value", 'key3': 3}
print (dictionary.values())

#keyとvalue同時に取得 [items]
keyとvalueのタプルのリストが返ってきます。

dictionary = {'key1': [645, 469], 'key2': "value", 'key3': 3}
print (dictionary.items())

    #-> [('key3', 3), ('key2', 'value'), ('key1', [645, 469])]

#iteritems()
for文とかで回すならこのメソッドを使用したほうが便利な場合があるかもです。

dictionary = {'key1': [645, 469], 'key2': "value", 'key3': 3}

for key, value in dictionary.iteritems():
	print "key:", key, "-- value:", value 

12
13
2

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
12
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?