Skip to content

Commit 0a06a3d

Browse files
author
Charlie Tonneslan
authored
Return an empty string from natural_list for an empty list (#318)
Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
1 parent 013969a commit 0a06a3d

2 files changed

Lines changed: 3 additions & 0 deletions

File tree

src/humanize/lists.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def natural_list(items: list[Any]) -> str:
2828
Returns:
2929
str: A string with commas and 'and' in the right places.
3030
"""
31+
if not items:
32+
return ""
3133
if len(items) == 1:
3234
return str(items[0])
3335
elif len(items) == 2:

tests/test_lists.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
([["one", "two", "three"]], "one, two and three"),
1313
([["one", "two"]], "one and two"),
1414
([["one"]], "one"),
15+
([[]], ""),
1516
([[""]], ""),
1617
([[1, 2, 3]], "1, 2 and 3"),
1718
([[1, "two"]], "1 and two"),

0 commit comments

Comments
 (0)