yoh, Sort dict

have a problem with sorting dict by value!!! found something interesting on http://code.activestate.com/recipes/304440/ by Nick Coghlan
let's see

>>d = {'a':0,'b':1,'c':2}
>>d

#will get
{'a':0,'c':2,'b':1}

some solution from Nick
>>from operator import itemgetter
>>print sorted(d.items(),key=itemgetter(1))

#we get
[('a':0),('b':1),('c':2)] # >>>>> a list of tuples

it might be useful in the future!!!:) thank nick and activestate

Comments

Post a Comment

Popular posts from this blog

Return Code 127