Home » Python » How can I get dictionary key as variable directly in Python (not by searching from value)?

How can I get dictionary key as variable directly in Python (not by searching from value)?

Posted by: admin November 1, 2017 Leave a comment

Questions:

Sorry for this basic question but my searches on this are not turning up anything other than how to get a dictionary’s key based on its value which I would prefer not to use as I simply want the text/name of the key and am worried that searching by value may end up returning 2 or more keys if the dictionary has a lot of entries… what I am trying to do is this:

mydictionary={'keyname':'somevalue'}
for current in mydictionary:

   result = mydictionary.(some_function_to_get_key_name)[current]
   print result
   "keyname"

The reason for this is that I am printing these out to a document and I want to use the key name and the value in doing this

I have seen the method below but this seems to just return the key’s value

get(key[, default])
Answers:

You should iterate over keys with:

for key in mydictionary:
   print "key: %s , value: %s" % (key, mydictionary[key])

Questions:
Answers:

If you want to print key and value, use the following:

for key, value in my_dict.iteritems():
    print key, value

Questions:
Answers:

The reason for this is that I am printing these out to a document and I want to use the key name and the value in doing this

Based on the above requirement this is what I would suggest:

keys = mydictionary.keys()
keys.sort()

for each in keys:
    print "%s: %s" % (each, mydictionary.get(each))

Questions:
Answers:

If the dictionary contains one pair like this:

d = {'age':24}

then you can get as

field, value = d.items()[0]

Questions:
Answers:

keys=[i for i in mydictionary.keys()] or
keys = list(mydictionary.keys())

Questions:
Answers:

As simple as that:

mydictionary={'keyname':'somevalue'}
result = mydictionary.popitem()[0]

You will your dictionary and should make a copy of it

Questions:
Answers:

Iterate over dictionary (i) will return the key, then using it (i) to get the value

for i in D:
    print "key: %s, value: %s" % (i, D[i])

Questions:
Answers:

For python 3
If you want to get only the keys use this. Replace print(key) with print(values) if you want the values.

for key,value in my_dict:
  print(key)

Questions:
Answers:

What I sometimes do is I create another dictionary just to be able whatever I feel I need to access as string. Then I iterate over multiple dictionaries matching keys to build e.g. a table with first column as description.

dict_names = {'key1': 'Text 1', 'key2': 'Text 2'}
dict_values = {'key1': 0, 'key2': 1} 

for key, value in dict_names.items():
    print('{0} {1}'.format(dict_names[key], dict_values[key])

You can easily do for a huge amount of dictionaries to match data (I like the fact that with dictionary you can always refer to something well known as the key name)

yes I use dictionaries to store results of functions so I don’t need to run these functions everytime I call them just only once and then access the results anytime.

EDIT: in my example the key name does not really matter (I personally like using the same key names as it is easier to go pick a single value from any of my matching dictionaries), just make sure the number of keys in each dictionary is the same