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
If you just want the dictionary key and don't care about the value, note that (key, ), = foo.items()
doesn't work. You do need to assign that value to a variable.
So you need (key, _), = foo.items()
Illustration in Python 3.7.2:
>>> foo = {'a': 1}
>>> (key, _), = foo.items()
>>> key
'a'
>>> (key, ), = foo.items()
Traceback (most recent call last):
File "", line 1, in
ValueError: too many values to unpack (expected 1)