Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting Python Dictionary based on Key? [duplicate]

I have created a python dictionary which has keys in this form :

11, 10, 00, 01, 20, 21, 31, 30

The keys are string

I would like to maintain my dictionary in these sorted order:

00, 10, 20, 30, 01, 11, 21, 31 

This is based on the second value of the key.

I tried this sorted(dict.items(), key = lambda s: s[1]) and got the keys like:

20, 30, 21, 31, 01, 11, 10, 00

Can somebody guide me?

like image 909
gizgok Avatar asked Sep 22 '13 21:09

gizgok


1 Answers

You almost had it, but the key is the first item of the tuple:

sorted(dict.items(), key=lambda s: s[0])
like image 172
Ry- Avatar answered Sep 20 '22 23:09

Ry-