Ordered dictionary

The OrderedDict is a subclass of the dictionary and it remembers the order in which the elements are added:

Syntax

d1 = collections.OrderedDict() 
d1 is ordered dictionary here.

Let's look at the comparison between dictionary and ordered dictionary:

import collections 
print 'Regular Dictionary'
d = {}
d['a']= 'SAS'
d['b']= 'PYTHON'
d['c']= 'R'

for k,v in d.items():
print k, ":",v

print 'n Ordered dictionary'

d1 = collections.OrderedDict()
d1['a']= 'SAS'
d1['b']= 'PYTHON'
d1['c']= 'R'

for k,v in d1.items():
print k, ":",v

Here, we create a normal dictionary and an ordered dictionary. Both the outputs are shown here:

As we could see, the ordered dictionary has retained the order in which the elements were added.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.133.160