I have this defaultdict and a string variable:
my_dict = defaultdict(<class 'str'>, {2: 'bear', 3: 'fish', 4: 'dog', 5: 'goat'})
str_match = "goat"
and I would like to pop the key in my_dict
which has the same value as str_match
, and insert it at the front. Something like this:
my_dict = defaultdict(<class 'str'>, {5: 'goat', 2: 'bear', 3: 'fish', 4: 'dog'})
So far I have:
for key, value in list(my_dict.items()):
if value == str_match:
my_dict.pop(key)
Is there a way I can accomplish this?