0
How can I sort a list of dictionaries by a certain key when within those dictionaries there is one with the key None
?
Example:
mylist = [{'category': 'director'}, {'category': 'manager'}, {'category': None}]
mylist.sort() # causa erro porque não se pode comprar dict < dict
# causa erro porque não se pode comprar str < NoneType
result = sorted(mylist, key=lambda c: c['category'])
The main idea is always to get the most important from the list with the following level of relevance:
director > manager
and manager > None