sqlalchemy/sqlalchemy · error · ValueError
value not in list
Error message
value not in list
What it means
Raised by _AssociationList.remove when the value is not present in the proxied list, exactly like Python's built-in list.remove which raises ValueError. The proxy iterates its members, compares via the getter, and if no match is found raises ValueError("value not in list").
Source
Thrown at lib/sqlalchemy/ext/associationproxy.py:1588
count += 1
return count
def extend(self, values: Iterable[_T]) -> None:
for v in values:
self.append(v)
def insert(self, index: int, value: _T) -> None:
self.col[index:index] = [self._create(value)]
def pop(self, index: int = -1) -> _T:
return self.getter(self.col.pop(index))
def remove(self, value: _T) -> None:
for i, val in enumerate(self):
if val == value:
del self.col[i]
return
raise ValueError("value not in list")
def reverse(self) -> NoReturn:
"""Not supported, use reversed(mylist)"""
raise NotImplementedError()
def sort(self) -> NoReturn:
"""Not supported, use sorted(mylist)"""
raise NotImplementedError()
def clear(self) -> None:
del self.col[0 : len(self.col)]
def __eq__(self, other: object) -> bool:
return list(self) == other
def __ne__(self, other: object) -> bool:View on GitHub (pinned to 5995834ee0)
Solutions
- Guard with membership: if value in user.keywords: user.keywords.remove(value).
- Catch ValueError and treat removal as already-done (idempotent).
- Prefer a helper that discards if present.
Example fix
# before
user.keywords.remove(kw) # raises if absent
# after
try:
user.keywords.remove(kw)
except ValueError:
pass # already absent Defensive patterns
Strategy: try-catch
Validate before calling
if value in user.keywords:
user.keywords.remove(value) Try / catch
try:
user.keywords.remove(value)
except ValueError:
pass # already absent; idempotent Prevention
- Check membership before calling remove on proxied lists.
- Wrap removes in try/except ValueError for idempotent cleanup.
- Drive removals from UI/queue logic that already verified presence.
When it happens
Trigger: user.keywords.remove(some_keyword) where some_keyword is not currently in the collection; calling remove twice; removing a value that was never appended.
Common situations: Idempotent cleanup code that removes an item without checking membership first; UI-driven remove operations; concurrent edits that already removed the value.
Related errors
- pop from an empty set
- attempt to assign sequence of size %s to extended slice of s
- %s objects are unhashable
- association proxy to a non-relationship intermediary is not
- The association proxy can't be used as a plain column expres
AI-assisted analysis of sqlalchemy/sqlalchemy@5995834ee0 (2026-08-07).
Data as JSON: /api/errors/62a670f2f83f38c4.
Report an issue: GitHub.