Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OrderedDict for older versions of python

Tags:

python

Ordered dictionaries are extremely useful structures, but unfortunately these are quite recent only working in versions from 3.1 and 2.7. How can I use an ordered dictionary in older versions?

like image 359
Casebash Avatar asked Oct 24 '09 05:10

Casebash


People also ask

Is OrderedDict obsolete?

No it won't become redundant in Python 3.7 because OrderedDict is not just a dict that retains insertion order, it also offers an order dependent method, OrderedDict. move_to_end() , and supports reversed() iteration*.

Is OrderedDict slower than dict?

OrderedDict is over 80% slower than the standard Python dictionary (8.6/4.7≈1.83).

What is OrderedDict in Python?

Python's OrderedDict is a dict subclass that preserves the order in which key-value pairs, commonly known as items, are inserted into the dictionary. When you iterate over an OrderedDict object, items are traversed in the original order. If you update the value of an existing key, then the order remains unchanged.

What is the difference between OrderedDict and dictionary in Python?

The OrderedDict is a subclass of dict object in Python. The only difference between OrderedDict and dict is that, in OrderedDict, it maintains the orders of keys as inserted. In the dict, the ordering may or may not be happen. The OrderedDict is a standard library class, which is located in the collections module.


2 Answers

I installed ordereddict on python 2.6 with pip

pip install ordereddict 
like image 195
Arthur Ulfeldt Avatar answered Sep 21 '22 04:09

Arthur Ulfeldt


According to the documentation, for Python versions 2.4 or later this code should be used. There is also some code from Raymond Hettinger, one of the contributors to the PEP. The code here is claimed to work under 2.6 and 3.0 and was made for the proposal.

like image 33
Casebash Avatar answered Sep 24 '22 04:09

Casebash