TheAlgorithms/Python · error · ValueError

No data matching given value

Error message

No data matching given value

What it means

DoublyLinkedList.delete(data) walks the chain and raises ValueError('No data matching given value') when it reaches the last node without finding an equal datum. This is a value-lookup failure, distinct from the positional IndexErrors the same class uses.

Source

Thrown at data_structures/linked_list/doubly_linked_list.py:166

            self.tail = self.tail.previous
            self.tail.next = None
        else:
            temp = self.head
            for _ in range(index):
                temp = temp.next
            delete_node = temp
            temp.next.previous = temp.previous
            temp.previous.next = temp.next
        return delete_node.data

    def delete(self, data) -> str:
        current = self.head

        while current.data != data:  # Find the position to delete
            if current.next:
                current = current.next
            else:  # We have reached the end an no value matches
                raise ValueError("No data matching given value")

        if current == self.head:
            self.delete_head()

        elif current == self.tail:
            self.delete_tail()

        else:  # Before: 1 <--> 2(current) <--> 3
            current.previous.next = current.next  # 1 --> 3
            current.next.previous = current.previous  # 1 <--> 3
        return data

    def is_empty(self):
        """
        >>> linked_list = DoublyLinkedList()
        >>> linked_list.is_empty()
        True
        >>> linked_list.insert_at_tail(1)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Catch ValueError specifically around delete-by-value calls and treat as 'not present'.
  2. Pre-check membership if the class offers __contains__, or compare types before calling.
  3. For empty-list safety, guard with len(linked_list) > 0 first.

Example fix

# before
linked_list.delete(user_value)  # ValueError if absent
# after
try:
    linked_list.delete(user_value)
except ValueError:
    pass  # value not present; nothing to remove
Defensive patterns

Strategy: try-catch

Validate before calling

# no built-in contains; manual pre-check
found = any(node == target for node in iter_over(dll)) if dll.head else False

Try / catch

try:
    dll.delete(value)
except ValueError:
    pass  # value not present — treat as idempotent no-op

Prevention

When it happens

Trigger: delete(x) where x is not equal (==) to any node's data; calling delete on an empty list (current starts at head=None, so it dereferences immediately); searching for a value whose type differs, e.g. delete('1') in a list of ints.

Common situations: Deduplication or removal driven by user input without prior membership check; type mismatches between stored and searched values (str vs int from JSON); deleting an item already removed by a concurrent iteration.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/d9ad76b68da6db75. Report an issue: GitHub.