ok, I feel stupid, but there must be a better way than this! (finding name of unique key in dict)

Dino dino at no.spam.ar
Fri Jan 20 11:16:33 EST 2023


On 1/20/2023 11:06 AM, Tobiah wrote:
> On 1/20/23 07:29, Dino wrote:
>>
> 
> This doesn't look like the program output you're getting.

you are right that I tweaked the name of fields and variables manually 
(forgot a couple of places, my bad) to illustrate the problem more 
generally, but hopefully you get the spirit.

         "value": cn,
         "a": cd[cn]["a"],
         "b": cd[cn]["b"]

Anyway, the key point (ooops, a pun) is if there's a more elegant way to 
do this (i.e. get a reference to the unique key in a dict() when the key 
is unknown):

cn = list(cd.keys())[0] # There must be a better way than this!

Thanks



More information about the Python-list mailing list