Tuesday, March 23, 2010

Playing around with Ordered Dictionary (collections.OrderedDict)

The new and cool feature added to Python 3.1.x family is the Ordered Dictionary. Some implementations have been lying around for sometime, it's good to see one made it into  the Python standard library.

Dictionaries are general purpose container data structure that are accessed through keys. It uses a hashing mechanism to achieve fast retrieval of information by key on typical cases. The annoying thing about standard dictionary dict object is that, whenever we retrieve the keys, it is not guaranteed to be sorted.
In Java platform, usually this dictionary is called map. java.util.Map provides interface to implementations of map such as java.util.HashMap, java.util.TreeMap.

Every value is associated with value, written in : pair.

Try to paste this code to your Python 3.1 interpreter:

[sourcecode lang="python"]<br />ud = dict()<br />ud['c']='1'<br />ud['b']='2'<br />ud['a']='3'<br />[/sourcecode]


This interactive session below highlight the thing:

[sourcecode lang="shell"]<br />>>> ud = dict()<br />>>> ud['c']='1'<br />>>> ud['b']='2'<br />>>> ud['a']='3'<br />>>> ud<br />{'a': '3', 'c': '1', 'b': '2'}<br />[/sourcecode]


Nice general purpose container for all of our applications. The dictionary keys doesn't display in sorted order.

Instead when we paste this code into Python interpreter:

[sourcecode lang="python"]<br />import collections as coll<br />od = coll.OrderedDict()<br />od['c']='1'<br />od['b']='2'<br />od['a']='3'<br />[/sourcecode]


We will get this result:

[sourcecode lang="shell"]<br />>>> import collections as coll<br />>>> od = coll.OrderedDict()<br />>>> od['c']='1'<br />>>> od['b']='2'<br />>>> od['a']='3'<br />>>> od<br />OrderedDict([('c', '1'), ('b', '2'), ('a', '3')])<br />[/sourcecode]


As we can see in the result above, the collections.OrderedDict objects displayed the keys according to the sequence that they were inserted. When an insertion done on an already existing key, the new insertion will overwrite the value but the sequence is left untouched. A deletion followed by addition will move the key to the end of the list, as show in the session below (continuation of the previous one).

[sourcecode lang="shell"]<br />>>> od['d']='4'<br />>>> od<br />OrderedDict([('c', '1'), ('b', '2'), ('a', '3'), ('d', '4')])<br />>>> od['b']='5'<br />>>> od<br />OrderedDict([('c', '1'), ('b', '5'), ('a', '3'), ('d', '4')])<br />>>> od.pop('a')<br />'3'<br />>>> od<br />OrderedDict([('c', '1'), ('b', '5'), ('d', '4')])<br />>>> od['a']='6'<br />>>> od<br />OrderedDict([('c', '1'), ('b', '5'), ('d', '4'), ('a', '6')])<br />>>><br />[/sourcecode]


I guess "battery included" still holds, or probably, "more organised battery included...".
Hooray for Python.

Python 3.1.2 Released (bug fix released)

Python 3.1.2 (bugfix release) is out now! We can download from this site: http://www.python.org/download/