Iterate over OrderedDict in Python
Simple example from collections import OrderedDict d = OrderedDict() d[‘a’] = 1 d[‘b’] = 2 d[‘c’] = 3 for key, value in d.items(): print key, value Output: a 1 b 2 c 3
Simple example from collections import OrderedDict d = OrderedDict() d[‘a’] = 1 d[‘b’] = 2 d[‘c’] = 3 for key, value in d.items(): print key, value Output: a 1 b 2 c 3
There’s no built-in method for doing this in Python 2. If you need this, you need to write a prepend() method/function that operates on the OrderedDict internals with O(1) complexity. For Python 3.2 and later, you should use the move_to_end method. The method accepts a last argument which indicates whether the element will be moved … Read more
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*. Moreover, equality comparisons with OrderedDict are order sensitive and this is still not the case for dict in Python 3.7, for example: >>> OrderedDict([(1,1), … Read more