-2

I try to sort the following dictionary that contains list as values

Let's say i have this dictionary


x={"George": [68, 0% to work, 70%], "Nikolas": [25, 29% to work, 94%], "Beat": [18, 100% to work, 10%] }


I want to sort this dictionary based on the first value in the list

I want the following list

"Beat": [18, 100% to work, 10%], "Nikolas": [25, 29% to work, 94%], "George": [68, 0% to work, 70%]

Risadinha
  • 16,058
  • 2
  • 88
  • 91
Theodosis
  • 792
  • 2
  • 7
  • 22

1 Answers1

2
>>> dict(sorted(x.items(), key=lambda item: item[1][0]))

out:

{'Beat': [18, '100% to work', '10%'], 'Nikolas': [25, '29% to work', '94%'], 'George': [68, '0% to work', '70%']}

ChrisOram
  • 1,254
  • 1
  • 5
  • 17