Python: writing empty dictionary with ordered strings
Please understand, I am trying not to search the default ordered dictionary like this question . Something similar to.
If I call OrderedDict from the collections module, I can do -
from collections import OrderedDict
my_ordered_dict = OrderedDict()
Then I can just set items like this that call the setitem function, where I guess the magic is happening -
my_ordered_dict[key1] = value1 my_ordered_dict[key2] = value2 my_ordered_dict[key3] = value3
And get a great OrderedDict -
>my_ordered_dict
{key1:value1, key2:value2, key3:value3...}
However, when I try to simply initialize my value of the pair value like this:
my_ordered_dict = {key1 : value1,
key2 : value2,
key3 : value3...}
The dictionary is losing order.
I can hack this path instead of initializing the list of tuples:
default = [ (key1, value1), (key2, value2), (key3, value3)]
for pair in default:
my_ordered_dict[pair[0]] = pair[1]
But it looks like I am missing something. Any advice?
source to share
It works:
my_ordered_dict = OrderedDict(( (key1, value1), (key2, value2), (key3, value3) ))
The above works because the argument we are using is a tuple and the tuple is ordered.
Again, because the tuple is ordered, this will work as well:
my_ordered_dict = OrderedDict() my_ordered_dict.update(( (key1, value1), (key2, value2), (key3, value3) ))
In contrast, this will not work:
my_ordered_dict = OrderedDict({key1:value1, key2:value2,})
The argument OrderedDict
above is an unordered dictionary. The entire order was lost when it was created.
It is interesting, however, that the order can be reordered in the unordered dictionary, as these examples from the doc 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)])
source to share
Whenever you write:
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
It will evaluate like a regular dictionary and lose its order immediately. In your example, you are overwriting my_ordered_dict
with a regular dictionary.
You can use a list of tuples instead:
OrderedDict([('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3')])
source to share
You need to initialize OrderedDict this way to create OrderedDict:
>>> dct = OrderedDict(((key1, value1), (key2, value2), (key3, value3)))
>>> type(dct)
<class 'collections.OrderedDict'>
those. a dict created this way creates a normal dict, not an OrderedDict:
>>> dct = {key1: value1, key2 : value2, key3 : value3}
>>> type(dct)
<type 'dict'>
Check the python docs for some good OrderedDict examples .
source to share