How to print a dictionary's key?

前端 未结 20 817
眼角桃花
眼角桃花 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:45

    Probably the quickest way to retrieve only the key name:

    mydic = {}
    mydic['key_name'] = 'value_name'
    
    print mydic.items()[0][0]
    

    Result:

    key_name
    

    Converts the dictionary into a list then it lists the first element which is the whole dict then it lists the first value of that element which is: key_name

提交回复
热议问题