Use the correct data type for the job. Your goal should be to have workable code, and not that you use the same data type everywhere.
If your dictionary contains only one key and one value, you can get either indexing:
key = list(d)[0] value = list(d.values())[0]
or get both:
key, value = list(d.items())[0]
list calls are needed because in Python 3, .keys() , .values() and .items() return dict requests, not lists.
Another option is to use sequence unpacking:
key, = d value, = d.values()
or for both at the same time:
(key, value), = d.items()
source share