Answer by Victoria Stuart for How to print a dictionary's key?
I'm adding this answer as one of the other answers here (https://stackoverflow.com/a/5905752/1904943) is dated (Python 2; iteritems), and the code presented -- if updated for Python 3 per the suggested...
View ArticleAnswer by Fouad Boukredine for How to print a dictionary's key?
In Python 3:# A simple dictionaryx = {'X':"yes", 'Y':"no", 'Z':"ok"}# To print a specific key (for example key at index 1)print([key for key in x.keys()][1])# To print a specific value (for example...
View ArticleAnswer by kgui for How to print a dictionary's key?
Make sure to dodictionary.keys()rather thandictionary.keys
View ArticleAnswer by Mason for How to print a dictionary's key?
Additionally you can use....print(dictionary.items()) #prints keys and valuesprint(dictionary.keys()) #prints keysprint(dictionary.values()) #prints values
View ArticleAnswer by PeaWagon for How to print a dictionary's key?
I looked up this question, because I wanted to know how to retrieve the name of "the key" if my dictionary only had one entry. In my case, the key was unknown to me and could be any number of things....
View ArticleAnswer by Fekete Sumér for How to print a dictionary's key?
Probably the quickest way to retrieve only the key name:mydic = {}mydic['key_name'] = 'value_name'print mydic.items()[0][0]Result: key_nameConverts the dictionary into a list then it lists the first...
View ArticleAnswer by Jeril for How to print a dictionary's key?
If you want to get the key of a single value, the following would help:def get_key(b): # the value is passed to the function for k, v in mydic.items(): if v.lower() == b.lower(): return kIn pythonic...
View ArticleAnswer by Eugene Soldatov for How to print a dictionary's key?
Or you can do it that manner:for key in my_dict: print key, my_dict[key]
View ArticleAnswer by Rob Wilson for How to print a dictionary's key?
dict = {'name' : 'Fred', 'age' : 100, 'employed' : True }# Choose key to print (could be a user input)x = 'name'if x in dict.keys(): print(x)
View ArticleAnswer by Brian Bruggeman for How to print a dictionary's key?
# highlighting how to use a named variable within a string:mapping = {'a': 1, 'b': 2}# simple method:print(f'a: {mapping["a"]}')print(f'b: {mapping["b"]}')# programmatic method:for key, value in...
View ArticleAnswer by ademar111190 for How to print a dictionary's key?
dic = {"key 1":"value 1","key b":"value b"}#print the keys:for key in dic: print key#print the values:for value in dic.itervalues(): print value#print key and valuesfor key, value in dic.iteritems():...
View ArticleAnswer by Don for How to print a dictionary's key?
key_name = '...'print "the key name is %s and its value is %s"%(key_name, mydic[key_name])
View ArticleAnswer by Matt Joiner for How to print a dictionary's key?
import pprintpprint.pprint(mydic.keys())
View ArticleAnswer by Ned Batchelder for How to print a dictionary's key?
Since we're all trying to guess what "print a key name" might mean, I'll take a stab at it. Perhaps you want a function that takes a value from the dictionary and finds the corresponding key? A reverse...
View ArticleAnswer by fdb for How to print a dictionary's key?
Try this:def name_the_key(dict, key): return key, dict[key]mydict = {'key1':1, 'key2':2, 'key3':3}key_name, value = name_the_key(mydict, 'key2')print 'KEY NAME: %s' % key_nameprint 'KEY VALUE: %s' % value
View ArticleAnswer by juanchopanza for How to print a dictionary's key?
A dictionary has, by definition, an arbitrary number of keys. There is no "the key". You have the keys() method, which gives you a python list of all the keys, and you have the iteritems() method,...
View ArticleAnswer by Dominic Santos for How to print a dictionary's key?
Hmm, I think that what you might be wanting to do is print all the keys in the dictionary and their respective values?If so you want the following:for key in mydic: print "the key name is"+ key +"and...
View ArticleAnswer by Ry- for How to print a dictionary's key?
What's wrong with using 'key_name' instead, even if it is a variable?
View ArticleAnswer by zenna for How to print a dictionary's key?
The name of the key 'key_name' is 'key_name', thereforeprint('key_name')or whatever variable you have representing it.
View ArticleHow to print a dictionary's key?
I would like to print a specific Python dictionary key:mydic = {}mydic['key_name'] = 'value_name'Now I can check if mydic.has_key('key_name'), but what I would like to do is print the name of the key...
View Article
More Pages to Explore .....