1

I have a dictionary with keys going from 'Cycle 1' up to to 'Cycle 14' with their associated values.

The dictionary does not return the keys in order.

I attempt to sort the keys as follows:

SortedKeys = sorted(c.items())

It returns keys in string order... i.e 'Cycle 1, Cycle 11, Cycle 12 ... Cycle 9'

How can I get it numerically ordered? I've searched around and it doesn't seem so easy to do.

arsenal88
  • 1,040
  • 2
  • 15
  • 32

1 Answers1

1

Try this instead:

SortedKeys = sorted(c.items(), key=lambda x: int(x[0].split()[1]))
jpp
  • 159,742
  • 34
  • 281
  • 339