So I have this dictionary, which I'm converting to OrderedDict. On conversion, I'd like to sort the OrderedDict by the order in which its keys appear in a separate tuple. Any other values I'd like to append to the end of the OrderedDict. Nonexistent keys in the ordering tuple should be ignored.
I think I have most of it, but I'm having trouble wrapping my brain around the lambda sorted functions. Can you help me iron it out?
from collections import OrderedDict
d = {
'spam': 'tasty',
'subtitle': 'A Subtitle',
'title': 'Test Title',
'foo': 'bar',
}
key_order = ('title', 'subtitle', 'non_in_dictionary')
ordered_dict = OrderedDict(sorted(d.items(), key=lambda ???? ))
should produce ordered_dict
:
{
'title': 'Test Title',
'subtitle': 'A Subtitle',
'spam': 'tasty',
'foo': 'bar',
}