-1
d={
    1:[1,2,3,4],
    2:[5,4],
    3:[5,6,7]
}

Given a dictionary, we need to sort the dictionary as per the length of values (in this case, as per length of lists)

Output should be as below:

{1:[1,2,3,4],3:[5,6,7],2:[5,4]}

Please help me with the code

Harshita
  • 11
  • 3

1 Answers1

0

Easy way if you are sure that your keys will be integers:

d = {key: value for key, value in sorted(d.items())}
Amir Afianian
  • 2,679
  • 4
  • 22
  • 46