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

前端 未结 14 1730
面向向阳花
面向向阳花 2020-12-04 06:19

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 us

相关标签:
14条回答
  • 2020-12-04 07:17

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

    0 讨论(0)
  • 2020-12-04 07:18

    If the dictionary contains one pair like this:

    d = {'age':24}
    

    then you can get as

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

    For Python 3.5, do this:

    key = list(d.keys())[0]
    
    0 讨论(0)
提交回复
热议问题