I have a list of dictionaries like this:
[{"foo" : "bar", "myKey" : "one"},
{"foo" : "bar", "myKey" : "two"},
{"foo" : "bar", "yourKey" : "three"}]
I'd like to sort it by a key in the dictionary if it exists.
featured = sorted(filesToWrite, key=lambda k: k["myKey"])
This doesn't work if "myKey"
doesn't exist. EDIT: If myKey
doesn't exist in the dictionary, I'd like it to appear at the end of the list.
I could loop through the list manually and do it myself but I'm sure there is a pythonic way to accomplish my goal without doing all that.