I wonder if this section of the code can be a separate function `sorted_dict_mapping`.