Let us consider a dictionary:
sample_dict={1:\'r099\',2:\'g444\',3:\'t555\',4:\'f444\',5:\'h666\'}
I want to re-order this dictionary in an
Using an OrderedDict or Eli's solution will probably be a good way to go, but for reference here is a simple way to obtain the list of values you want:
[sample_dict[k] for k in desired_order_list]
If you aren't completely sure that every element from desired_order_list
will be a key in sample_dict
, use either [sample_dict.get(k) ...]
or [... for k in desired_order_list if k in sample_dict]
. The first method will put None
in for missing keys, the second method will only include values from the keys are are in the dict.