Skip to content

Commit

Permalink
Merge pull request #272 from jcrist/topk_key
Browse files Browse the repository at this point in the history
Fix topk to work with 0 for key
  • Loading branch information
mrocklin committed Aug 19, 2015
2 parents ee78566 + dd87e4f commit d1e3041
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion toolz/itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ def topk(k, seq, key=None):
See also:
heapq.nlargest
"""
if key and not callable(key):
if key is not None and not callable(key):
key = getter(key)
return tuple(heapq.nlargest(k, seq, key=key))

Expand Down
2 changes: 2 additions & 0 deletions toolz/tests/test_itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,8 @@ def test_topk():
assert topk(2, [{'a': 1, 'b': 10}, {'a': 2, 'b': 9},
{'a': 10, 'b': 1}, {'a': 9, 'b': 2}], key='b') == \
({'a': 1, 'b': 10}, {'a': 2, 'b': 9})
assert topk(2, [(0, 4), (1, 3), (2, 2), (3, 1), (4, 0)], 0) == \
((4, 0), (3, 1))


def test_topk_is_stable():
Expand Down

0 comments on commit d1e3041

Please sign in to comment.