4

Please realize I'm trying not looking for a default ordered dictionary like this question. Something similar.

If I call an OrderedDict from the collections module I can do -

 from collections import OrderedDict
 my_ordered_dict = OrderedDict()

Then I can just set items like this which calls on the setitem function, where I assume the magic happens -

 my_ordered_dict[key1] = value1
 my_ordered_dict[key2] = value2
 my_ordered_dict[key3] = value3

And get a perfectly OrderedDict -

>my_ordered_dict
 {key1:value1, key2:value2, key3:value3...}

However, when I try to just initilize my key value pairing like this:

my_ordered_dict = {key1 : value1,
                   key2 : value2,
                   key3 : value3...}

The dictionary loses order.

I can hack my way around this by instead of initializing a list of tuples:

default = [ (key1, value1), (key2, value2), (key3, value3)]

for pair in default:
   my_ordered_dict[pair[0]] = pair[1]

But it seems like i'm missing something. Any tips?

Community
  • 1
  • 1
jwillis0720
  • 4,329
  • 8
  • 41
  • 74

4 Answers4

4

This works:

my_ordered_dict = OrderedDict(( (key1, value1), (key2, value2), (key3, value3) ))

The above works because the argument we are using is tuple and a tuple is ordered.

Again because a tuple is ordered, this will also work :

my_ordered_dict = OrderedDict()
my_ordered_dict.update(( (key1, value1), (key2, value2), (key3, value3) ))

By contrast, this will not work:

my_ordered_dict = OrderedDict({key1:value1, key2:value2,})

The argument to OrderedDict above is an unordered dictionary. All order was lost when it was created.

Interestingly, though, order can be reimposed on an unordered dictionary as these examples from the documention for the collections module show:

>>> # dictionary sorted by key
>>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))
OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])

>>> # dictionary sorted by value
>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])

>>> # dictionary sorted by length of the key string
>>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])
John1024
  • 109,961
  • 14
  • 137
  • 171
2

Whenever you write:

{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

It will be evaluated as a normal dictionary and immediately lose its order. In your example, you are overwriting my_ordered_dict with a normal dictionary.

Instead, you could use a list of tuples:

OrderedDict([('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3')])
grc
  • 22,885
  • 5
  • 42
  • 63
  • @levi It doesn't maintain the order of the keys, since you are passing them through a normal dictionary. – grc Aug 09 '14 at 06:00
1

You need to initialise the OrderedDict like this to create an OrderedDict:

>>> dct = OrderedDict(((key1, value1), (key2, value2), (key3, value3)))
>>> type(dct)
<class 'collections.OrderedDict'>

that is, a dict created like this produces a normal dict and not an OrderedDict:

>>> dct = {key1: value1, key2 : value2, key3 : value3}
>>> type(dct)
<type 'dict'>

Check the python docs for some good OrderedDict examples.

krock
  • 28,904
  • 13
  • 79
  • 85
0

If you use:

my_ordered_dict = {key1 : value1,
                   key2 : value2,
                   key3 : value3...}

That is a ordinary(unsorted) dict, you need to do this:

my_ordered_dict = OrderedDict(((key1, value1), (key2, value2), (key3, value3)))
levi
  • 22,001
  • 7
  • 73
  • 74