{"record":{"id":"d691c833be2bb2b7","repo":"TheAlgorithms/Python","slug":"list-is-empty","errorCode":null,"errorMessage":"List is empty","messagePattern":"List is empty","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"data_structures/linked_list/deque_doubly.py","lineNumber":72,"sourceCode":"        successor._prev = predecessor\n        self._size -= 1\n        temp = node._data\n        node._prev = node._next = node._data = None\n        del node\n        return temp\n\n\nclass LinkedDeque(_DoublyLinkedBase):\n    def first(self):\n        \"\"\"return first element\n        >>> d = LinkedDeque()\n        >>> d.add_first('A').first()\n        'A'\n        >>> d.add_first('B').first()\n        'B'\n        \"\"\"\n        if self.is_empty():\n            raise Exception(\"List is empty\")\n        return self._header._next._data\n\n    def last(self):\n        \"\"\"return last element\n        >>> d = LinkedDeque()\n        >>> d.add_last('A').last()\n        'A'\n        >>> d.add_last('B').last()\n        'B'\n        \"\"\"\n        if self.is_empty():\n            raise Exception(\"List is empty\")\n        return self._trailer._prev._data\n\n    # DEque Insert Operations (At the front, At the end)\n\n    def add_first(self, element):\n        \"\"\"insertion in the front","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/linked_list/deque_doubly.py#L54-L90","documentation":"LinkedDeque.first() raises a bare Exception('List is empty') when the deque has no elements, because it would otherwise dereference the sentinel's _next into the trailer. It is the accessor counterpart to last(); both are read-only peeks and do not modify the deque.","triggerScenarios":"Calling first() on a freshly constructed LinkedDeque, or after remove_first()/remove_last() have drained all previously added elements.","commonSituations":"Producer/consumer code that peeks before checking is_empty(); porting code from collections.deque (where deque[0] raises IndexError, not Exception) and catching the wrong type; test setup that assumes the deque was pre-populated.","solutions":["Guard the call with 'if not d.is_empty():' before peeking.","Catch the exception with 'except Exception as e: if str(e) == \"List is empty\"' — note it is a generic Exception, not IndexError.","Track the element count on the caller side so peek is only attempted when elements are known to exist."],"exampleFix":"# before\nhead = d.first()  # Exception: List is empty\n# after\nhead = d.first() if not d.is_empty() else None","handlingStrategy":"validation","validationCode":"front = d.first() if not d.is_empty() else None","typeGuard":null,"tryCatchPattern":"try:\n    front = d.first()\nexcept Exception as e:\n    if 'List is empty' not in str(e):\n        raise\n    front = None","preventionTips":["Treat first() as conditional: always pair with is_empty().","Note this library raises bare Exception, not IndexError, for empty peeks.","In tests, assert the empty behavior explicitly since the doctests document it."],"tags":["deque","empty-collection","peek","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}