Say I want to create a sorted dictionary (using OrderedDict
), based on a rather complex operation on the values:
sorting_function = lambda x: x[1][0]
my_dictionary = OrderedDict(sorted(my_dictionary.items(), key=sorting_function))
The problem with the code above is that, as I have it now, for some keys, my dictionary holds the value None
, so the lambda operator throws an exception.
I would like these entries to be placed last in my dictionary, but I don't know how to modify my lambda function to handle exceptions. Do I need to resort to a full fledged function for this? Any thoughts?
Update:
To clarify, x[1][0]
returns a string in my dictionary, when the entry is not None