1

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}]
Anthony
  • 33,838
  • 42
  • 169
  • 278
  • 2
    Gah, that is by Key, you actually want the "by value" question: http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value Answer is the same though: Dictionaries can't be sorted. Look at `OrderedDict` – Andy Aug 30 '16 at 19:57
  • Is it possible to convert it to a list based on size element? – Anthony Aug 30 '16 at 19:58
  • Ordinary dictionaries can't be order. You need to use a [`collections.OrderedDict`](https://docs.python.org/2/library/collections.html#collections.OrderedDict) instead. – martineau Aug 30 '16 at 19:58
  • @Andy I updated the question. I don't think it is a duplicate any longer – Anthony Aug 30 '16 at 20:00
  • What if you had `"size":-3` in another dict? – Padraic Cunningham Aug 30 '16 at 21:20
  • Dictionaries cannot be ordered or indexed, since its a collection of key-value-pair – chenchuk Aug 30 '16 at 23:19

3 Answers3

2

You can use an OrderedDict

from collections import OrderedDict

d = {'c': {'icecream': 'orange', 'fruit': 'apple', 'size': 1},'a': {'foo': 'bar', 'something': 'else', 'size': 0}, 'd': {'computer': 'mac', 'size': -1}}

print (OrderedDict(sorted(d.items(), key=lambda t: t[1]['size'] if t[1]['size']>=0 else float("inf") )))

As you want -1 to be the last one, just set the key to infinite (float["inf"]) in this case.

rafaelc
  • 57,686
  • 15
  • 58
  • 82
0

Dictionary is unordered. That means you can not sort it and keep it in order.

However, if you wish to iterate it in order (as a list containing tuple of the key and value), you can use this:

>>> d = {'c': {'icecream': 'orange', 'fruit': 'apple', 'size': 1},'a': {'foo': 'bar', 'something': 'else', 'size': 0}, 'd': {'computer': 'mac', 'size': -1}}
>>> sorted = sorted(d.items(), key=lambda x: x[1]['size'])
>>> sorted
[('d', {'computer': 'mac', 'size': -1}), ('a', {'something': 'else', 'foo': 'bar', 'size': 0}), ('c', {'fruit': 'apple', 'icecream': 'orange', 'size': 1})]

You can easily transform this list into a dictionary back, by creating a dictionary and adding all of this values:

>>> g = {}
>>> for x in sorted:
...     g[x[0]] = x[1]
...
>>> g
{'d': {'size': -1, 'computer': 'mac'}, 'a': {'something': 'else', 'size': 0, 'foo': 'bar'}, 'c': {'fruit': 'apple', 'icecream': 'orange', 'size': 1}}
Uriel
  • 15,579
  • 6
  • 25
  • 46
-1

Python dictionaries are unordered.

Use OrderedDict instead.

crait
  • 156
  • 1
  • 4
  • 15