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?
itemsreturns list of tuples where first item is key and second is value. So if You would like to sort them by keys You should use sorted(dict.items(), key = lambda s: s[0]) but in this case You got keys ascending sorted If You would like to sort as in your example 11, 10, 00, 01, 20, 21, 31, 30 > I would like to maintain my dictionary in these sorted order: 00, 10, 20, 30, 01, 11, 21, 31 You should reverse your key in sorted function sorted(dict.items(), key = lambda s: ''.join(reversed(s[0])))