I have a bunch of dictionaries with only one key-value pairs that are sometimes generated by one of the scripts I wrote, and I need to get rid of them.
To do this, I need to get the key and value of the dict. Minimal reproducible example to illustrate my intent:
d = {'a': 0}
assert len(d) == 1
k, v = list(d.items())[0]
Don’t know if this is a duplicate, but I can’t find an answer using Google.
Is there a simpler way to get k, v from d?
>Solution :
If you don’t need the dict anymore:
k, v = d.popitem()
If you do:
(k, v), = d.items()