May 2017
Intermediate to advanced
280 pages
6h 2m
English
We can sort the ordered dictionary using the sorted() function:
Syntax
dict = collections.OrderedDict(sorted(d1.items()))dict = New sorted dictionaryd1= Original Order dictionary
Let's take an example to understand the sorted() function. For a refresher on sorted() function, you can revisit Chapter 4, Lists:
import collections print 'n Order dictionary'd1 = collections.OrderedDict()d1['a']= 'SAS'd1['d']= 'PYTHON'd1['b']= 'JULIA'd1['f']= 'R'd1['c']= 'SPARK'for k,v in d1.items(): print k, ":",vprint 'n Sorted Order dictionary'dict = collections.OrderedDict(sorted(d1.items()))for k,v in dict.items(): print k, ":",v
Here we create an ordered dictionary d1 and then sort it using the sorted() ...
Read now
Unlock full access