How to extract dictionary single key-value pair in variables

后端 未结 10 1724
别那么骄傲
别那么骄傲 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条回答
  •  猫巷女王i
    2020-12-08 07:04

    This is best if you have many items in the dictionary, since it doesn't actually create a list but yields just one key-value pair.

    k, v = next(d.iteritems())
    

    Of course, if you have more than one item in the dictionary, there's no way to know which one you'll get out.

提交回复
热议问题