0

My question is related to the answer of this one:
Sort a Python dictionary by value

And for those wishing to sort on keys instead of values:

import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(0))

How do I sort on keys like above when my keys are tuples? For example, A: {(-2929109664628200379, 10): table, (-3884855813103006483, 15): chair}, sorting using the second number from the key.

Edit
Made it by creating a function getKey to return the value I wanted, now I realize it was a trivial question, thank you @jonrsharpe

Community
  • 1
  • 1
  • 1
    There are dozens of questions on sorting with `key`; all you need is a function that maps the input (in this case, a tuple `((keypart1, keypart2), value)`) to the thing you want to sort on (`keypart2`). This is trivial. – jonrsharpe Jan 06 '16 at 21:36

1 Answers1

0

Just sort the keys and do a lookup, sorting the keys by the second element:

d = {(-2929109664628200379, 10): "table", (-3884855813103006483, 15): "chair"}

sted = [(k, d[k]) for k in sorted(d,key=itemgetter(1))]
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321