, asked
Views : 31

How to create Ordered dictionaries in Python?

How to create Ordered dictionaries in Python?                                        

1 Answer
Jayashree

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)

Advertisements
Related Questions