I want to sort a list by a dictionary, as shown below:
The list:
L = ['Jack', 'King', '9', 'King', '10']
The dictionary:
D = {0:'Ace', 1:'King', 2:'Queen', 3:'Jack', 4:'10', 5:'9'}
I want to sort L based on keys of dictionary D, such that the output would be:
[ 'King', 'King', 'Jack', '10', '9' ]
I tried:
sorted(L, key = D.get)
but got error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in
----> 3 sorted(L, key = D.get)
TypeError: '<' not supported between instances of 'NoneType' and 'NoneType'
Why did I get this error? Aren't the dictionary keys integers?
Update:
Similar question: Reverse / invert a dictionary mapping