I have a dictionary like this:
>>> d
{'c': {'icecream': 'orange', 'fruit': 'apple', 'size': 1},'a': {'foo': 'bar', 'something': 'else', 'size': 0}, 'd': {'computer': 'mac', 'size': -1}}
How can I order the elements of this dictionary by size
with the exception that item with size
-1
comes in the end.
So the above dictionary would turn to:
>>> converted
{'a': {'foo': 'bar', 'something': 'else', 'size': 0}, 'c': {'icecream': 'orange', 'fruit': 'apple', 'size': 1}, 'd': {'computer': 'mac', 'size': -1}}
Update
Since dictionaries can not be ordered.
Is it possible to convert the above to a list with dictionary? i.e.
>>> converted_to_list
[{'foo': 'bar', 'something': 'else', 'size': 0}, {'icecream': 'orange', 'fruit': 'apple', 'size': 1}, {'computer': 'mac', 'size': -1}]