1

I am trying to sort a dictionary (called "size_dict") by value and print the first 20 elements to the console. I tried the following code:

print(sorted(size_dict.items())[:20])

Somehow that does not work as I get the following output:

[((64, 30), 1), ((107, 61), 1), ((175, 75), 1), ((183, 105), 1), ((207, 118), 1), ((219, 94), 2), ((220, 125), 1), ((223, 98), 1), ((226, 97), 1), ((229, 132), 1), ((230, 132), 1), ((233, 134), 2), ((235, 101), 1), ((236, 135), 1), ((240, 137), 4), ((243, 139), 1), ((244, 105), 2), ((245, 105), 1), ((247, 106), 1), ((248, 142), 1)]

Does anybody have an idea what I am doing wrong here?

MyLion
  • 61
  • 5

1 Answers1

0

Something as simple as this will do:

print(sorted(size_dict.items(), key=lambda f: f[1])[:20])
Anatolii
  • 14,139
  • 4
  • 35
  • 65