Sorting a dict with tuples as values

keys() only gives you keys, not values, so you have to use the keys to retrieve values from the dict if you want to sort on them:

return sorted(self.diffs.keys(), key=lambda x: self.diffs[x], reverse=True)

Since you're sorting on rank, which is the first item in the tuple, you don't need to specify which item in the value tuple you want to sort on. But if you wanted to sort on raw_data1:

return sorted(self.diffs.keys(), key=lambda x: self.diffs[x][1], reverse=True)

You're passing the key as the argument to, uh, key.

[k for (k, v) in sorted(D.iteritems(), key=lambda x: x[1], reverse=True)]