0

I have a dictionary as follows

pj = {'S_P': 42.0, 'S_SB': 30.0, 'C_G': 17.0, 'T_G': 25.0, 'C_O': 34.0, 'C_P': 39.0, 'C_SB': 20.0, 'T_O': 39.0}

i want to arrange this dictionary as i.e in ascending order

pj = {'C_G': 17.0, 'C_SB': 20.0,'T_G': 25.0,'S_SB': 30.0,'C_O': 34.0,'C_P': 39.0, , 'T_O': 39.0 ,'S_P': 42.0}

To achieve this what can i do

i have tried using

print sorted(pj.values())

but i get output as

[17.0, 20.0, 25.0, 30.0, 34.0, 39.0, 39.0, 42.0]

I am using python 2.7

Dr Dre
  • 3
  • 4
  • Dictionaries are *unordered*. You can produce a sorted sequence of `(key, value)` pairs, from which you can create a `OrderedDict` object perhaps. See the duplicate. – Martijn Pieters Feb 11 '16 at 07:54
  • @MartijnPieters I have already read that link u provided. it is really confusing. can u plzz suggest me just one method – Dr Dre Feb 11 '16 at 07:57
  • I already did. If you *did* read another post on Stack Overflow, **then share that information**. Show us how that post didn't help you and why. – Martijn Pieters Feb 11 '16 at 08:00
  • However, without a use case (*why* do you need your dictionary in sorted order?) there is very little we can do to help you anyway. – Martijn Pieters Feb 11 '16 at 08:03
  • I tried this d_sorted = {} d_sorted = sorted(zip(pj.keys() , pj.values())) but output became [('C_G', 17.0), ('C_O', 34.0), ('C_P', 39.0), ('C_SB', 20.0), ('S_P', 42.0), ('S_SB', 30.0), ('T_G', 25.0), ('T_O', 39.0)] – Dr Dre Feb 11 '16 at 08:04
  • @MartijnPieters dictionary changed to something else – Dr Dre Feb 11 '16 at 08:05

0 Answers0