Sorting List Elements by Dictionary Keys

  • Share this:

Code introduction


This function sorts the elements of a list based on the weights defined in a dictionary


Technology Stack : list comprehension, lambda expression, sorted function, exception handling

Code Type : Sort function

Code Difficulty : Intermediate


                
                    
def aordn(arg1, arg2):
    try:
        return sorted(arg1, key=lambda x: arg2[x])
    except TypeError:
        return "Error: Arguments must be a list and a dictionary with corresponding keys"