TheAlgorithms/Python · error · ValueError
the words should be a list of non-empty strings
Error message
the words should be a list of non-empty strings
What it means
Raised by word_break() when words is not a list or any element is not a non-empty string. The dictionary is inserted into a trie keyed by individual characters, so empty strings would create degenerate nodes and non-str elements would break char iteration. Both wrong-container and wrong-element cases produce this single message.
Source
Thrown at dynamic_programming/word_break.py:64
ValueError: the string should be not empty string
>>> word_break('abc', [123])
Traceback (most recent call last):
...
ValueError: the words should be a list of non-empty strings
>>> word_break('abc', [''])
Traceback (most recent call last):
...
ValueError: the words should be a list of non-empty strings
"""
# Validation
if not isinstance(string, str) or len(string) == 0:
raise ValueError("the string should be not empty string")
if not isinstance(words, list) or not all(
isinstance(item, str) and len(item) > 0 for item in words
):
raise ValueError("the words should be a list of non-empty strings")
# Build trie
trie: dict[str, Any] = {}
word_keeper_key = "WORD_KEEPER"
for word in words:
trie_node = trie
for c in word:
if c not in trie_node:
trie_node[c] = {}
trie_node = trie_node[c]
trie_node[word_keeper_key] = True
len_string = len(string)
# Dynamic programming methodView on GitHub (pinned to f5988cc097)
Solutions
- Normalize to a list of non-empty strings: words = [w for w in words if isinstance(w, str) and w].
- Wrap sets/tuples: word_break(s, list(words)) after filtering empties.
- Sanitize file-loaded wordlists by stripping and dropping blank lines.
Example fix
# before
word_break('abc', {'a', 'b', ''}) # ValueError (set + empty string)
# after
word_break('abc', [w for w in ['a', 'b', ''] if w]) Defensive patterns
Strategy: validation
Validate before calling
def valid_words(words: object) -> bool:
return isinstance(words, list) and all(
isinstance(w, str) and len(w) > 0 for w in words
) Type guard
def word_list(words: object) -> TypeGuard[list[str]]:
return isinstance(words, list) and all(
isinstance(w, str) and w for w in words
) Prevention
- Wrap sets/tuples with list(...) before calling.
- Filter blank entries when loading wordlists: [w for w in raw if w.strip()].
- Keep dictionary data as a list[str] end to end.
When it happens
Trigger: Calling word_break('abc', ['']) or word_break('abc', [123]) or word_break('abc', 'abc') (a bare string is not a list); a tuple of words also fails since isinstance(words, list) is checked strictly.
Common situations: Deduplicating words into a set or tuple and passing it directly; loading a wordlist where blank lines become '' after .strip(); mixed-type lists from untyped JSON input.
Related errors
- the string should be not empty string
- Limit for the Catalan sequence must be ≥ 0
- Negative arguments are not supported
- iterations must be defined as integers
- starting number must be and integer
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/dd8dc2811597cbaa.
Report an issue: GitHub.