0

For a school assignment I need to list and sort values from a nested dictionary. Here's the dictionary:

dict = {"jan": [-2.9, 220.3], "feb": [-2, 202.6], "mar": [0.1, 129.5], 
"apr": [4.6, 114.5], "may": [9.4, 106.8], "jun": [14, 40.1], 
"jul": [16.2, 25.6], "aug": [15.5, 23.8], "sep": [11.8, 91.7], 
"oct": [6.8, 103.1],"nov": [2.6, 152.1], "dec": [-0.4, 241.3]}

I want the sorted list to look something like this:

-2.9, -2, 0.4, 0.1, 2.6, 4.6, etc. 

I tried:

print(sorted(dict.values()))

but got this instead:

[[-2.9, 220.3], [-2, 202.6], [-0.4, 241.3], [0.1, 129.5], [2.6, 152.1], etc.

So, is there a way to list one value without the other pair?

imadelasagna
  • 1
  • 1
  • 1

1 Answers1

1

Just u need to make a little tweak in you code. Because you just want to sort based on 0th index value in dict.values()

UPDATE :

print(sorted(dict.values()))

TO :

print(sorted(i[0] for i in dict.values()))
Davinder Singh
  • 2,060
  • 2
  • 7
  • 22