Goal: create a new OrderedDict instance's keys in such way that keys in the specified order list appear first. If some keys in order list are not present in OrderedDict, ignore them. Additionally, reordering should be stable, that is, if some keys were not specified in order list, they should keep their original order from the old OrderedDict. In other words:o = OrderedDict([('a',1), ('b',2), ('d', 5), ('c',3), ('f', 6)])reorder_ordereddict(o, ['c', 'a', 'x'])produces:OrderedDict([('c', 3), ('a', 1), ('b', 2), ('d', 5), ('f', 6)])Code:def reorder_ordereddict(od, new_key_order):    odkeys = od.keys()    fullorder = [x for x in new_key_order if x in odkeys]    remaining = [x for x in odkeys if not x in fullorder]    fullorder.extend(remaining)    newod = OrderedDict([(k,None) for k in fullorder])    newod.update(od)    return newodAny better ideas how to do this?
Reordering OrderedDict
python;hash table
You can simplify it to this:def reorder_ordereddict(od, new_key_order):    new_od = OrderedDict([(k, None) for k in new_key_order if k in od])    new_od.update(od)    return new_odThe first line establishes the required key order, being careful not to introduce keys that don't already exist.The second line takes care of the rest.  The existing keys not explicitly mentioned should come out in the correct order when update() iterates through the keys of od.