How to extract dictionary single key-value pair in variables

后端 未结 10 1697
别那么骄傲
别那么骄傲 2020-12-08 06:33

I have only a single key-value pair in a dictionary. I want to assign key to one variable and it\'s value to another variable. I have tried with below ways but I am getting

10条回答
  •  独厮守ぢ
    2020-12-08 07:23

    d = {"a": 1}
    

    you can do

    k, v = d.keys()[0], d.values()[0]
    

    d.keys() will actually return list of all keys and d.values() return list of all values, since you have a single key:value pair in d you will be accessing the first element in list of keys and values

提交回复
热议问题