How to print a dictionary's key?

前端 未结 20 724
眼角桃花
眼角桃花 2020-11-27 09:22

I would like to print a specific Python dictionary key:

mydic = {}
mydic[\'key_name\'] = \'value_name\'

Now I can check if mydic.has_

相关标签:
20条回答
  • 2020-11-27 09:58

    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 its value is" + mydic[key]
    

    Make sure you use +'s instead of ,' as well. The comma will put each of those items on a separate line I think, where as plus will put them on the same line.

    0 讨论(0)
  • 2020-11-27 09:59

    The name of the key 'key_name' is key_name, therefore print 'key_name' or whatever variable you have representing it.

    0 讨论(0)
提交回复
热议问题