How to create Ordered dictionaries in Python?

PythonServer Side ProgrammingProgramming

An OrderedDict is a dictionary subclass that remembers the order in which its contents are added, It is defined in collections module of Python library. OrderDict remembers the order of addition of key-value pairs in a dictionary

>>> from collections import OrderedDict
>>> od=OrderedDict(d.items())
>>> od
OrderedDict([('banana', 3), ('apple', 4), ('pear', 1), ('orange', 2)])
>>> t=od.popitem()
>>> t
('orange', 2)
raja
Published on 06-Mar-2018 20:10:06
Advertisements