If the lists are really big:
userd = {d['id']:d for d in users} sortedusers = [userd.get(o) for o in order]
This is O(2n) . A solution using only sort would be O(n^3.log(n)) (sort nlogn , with a search for each identifier in the list equal to O(n^2) ), which is clearly worse for large lists. For smaller lists (for example, 3 items), the low overhead of not creating a new data structure will make it faster; conversely, if you continue sorting by the new order specifications, the overhead of creating a new dict is quickly amortized.
source share