diff --git a/toolz/itertoolz.py b/toolz/itertoolz.py index 354ecf25..f8143345 100644 --- a/toolz/itertoolz.py +++ b/toolz/itertoolz.py @@ -332,6 +332,11 @@ def tail(n, seq): drop take """ + if n == 0: + try: + return seq[:0] + except (TypeError, KeyError): + return () try: return seq[-n:] except (TypeError, KeyError): diff --git a/toolz/tests/test_itertoolz.py b/toolz/tests/test_itertoolz.py index c8640917..f505153f 100644 --- a/toolz/tests/test_itertoolz.py +++ b/toolz/tests/test_itertoolz.py @@ -189,6 +189,11 @@ def test_tail(): assert list(tail(3, 'ABCDE')) == list('CDE') assert list(tail(3, iter('ABCDE'))) == list('CDE') assert list(tail(2, (3, 2, 1))) == list((2, 1)) + assert tail(0, [10, 20, 30]) == [] + assert list(tail(0, [10, 20, 30])) == [] + assert tuple(tail(0, iter([10, 20, 30]))) == () + assert tail(0, 'ABCDE') == '' + assert tail(0, (3, 2, 1)) == () def test_drop():