1

I'm using lambda to sort a dict.

Following is what I have:

sorted = sorted(foo_dict, key=lambda foo: my_method(foo, bar)) 

It works but I need to exclude dict elements for which my_method(foo, bar) returns a value that is greater than a pre-defined limit.

How can I do this?

hnroot
  • 1,064
  • 2
  • 13
  • 23
  • 3
    `filter(..., key=lambda foo: my_method(foo, bar) < threshold)`. Using `sorted` as a name is a bad idea. It will mask the builtin – Moses Koledoye Jun 22 '16 at 13:11
  • 1
    How are you sorting dict? Dicts do not have an order... I assume you either have an [Ordered Dict](https://docs.python.org/dev/whatsnew/2.7.html#pep-372-adding-an-ordered-dictionary-to-collections) or you want it to return a [list of tuples that represent a dict](http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value)? – R Nar Jun 22 '16 at 13:12

0 Answers0