Sorting List by Dictionary Values

  • Share this:

Code introduction


Sorts a list `arg1` based on the values in the dictionary `arg2`. If `arg2` is missing a key from `arg1`, a ValueError is raised.


Technology Stack : Built-in function sorted, lambda expression, exception handling

Code Type : Function

Code Difficulty : Intermediate


                
                    
def aord(arg1, arg2):
    try:
        return sorted(arg1, key=lambda x: arg2[x])
    except KeyError:
        raise ValueError("arg2 must contain all keys from arg1")