Consider some mapping my_map
that defines the order of some keys, and some dictionary my_dict
that maps the same keys into some values:
my_map = {'x' : 2, 'y' : 0, 'z' : 1}
my_dict = {'x' : 'foo', 'z' : 'bar', 'y' : 'baz'}
I want to get an ordered list of the values of my_dict
using the order defined by my_map
. My best approach of getting there is:
inv_map = {v: k for k, v in my_map.items()}
ordered_list = [my_dict[k] for k in [inv_map[d] for d in range(len(my_map))]]
Is there a less clunky way of doing the same?
question from:https://stackoverflow.com/questions/65928121/simplify-code-using-an-inverse-dictionary-in-python