Another possibility is to be creative with indexing without need to convert dictionary.
'Get index of needed value in list of dictionary values and find key with same index in list of dictionary keys':
Follow-up edit: so one can utilize this something like following:
'Get index of needed value in list of dictionary values and find key with same index in list of dictionary keys':
>>> d = {1: 'ham', 2: 'eggs', 3: 'bacon'} >>> value = 'eggs' >>> [*d.keys()][[*d.values()].index(value)] # instead of *d.keys() one may use *d 2This way it is possible to have non-hashable values but still find corresponding key:
>>> d = {1: ['ham'], 2: ['eggs'], 3: ['bacon']} >>> value = ['bacon'] >>> [*d.keys()][[*d.values()].index(value)] 3NB! If there are several equal values this will return first one encountered.
Follow-up edit: so one can utilize this something like following:
>>> dct = {'1': 'one', '2': 'two', '3': 'three'} >>> lst = [ ... ['one', 'uno'], ... ['two', 'dos'], ... ['three', 'tres'] ... ] >>> for row in lst: ... row[0] = [*dct][[*dct.values()].index(row[0])] ... >>> lst [['1', 'uno'], ['2', 'dos'], ['3', 'tres']]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.