MCPcopy
hub / github.com/keon/algorithms / peek

Method peek

algorithms/data_structures/stack.py:186–197  ·  view source on GitHub ↗

Return the top element without removing it. Returns: The top element. Raises: IndexError: If the stack is empty.

(self)

Source from the content-addressed store, hash-verified

184 return value
185
186 def peek(self) -> object:
187 """Return the top element without removing it.
188
189 Returns:
190 The top element.
191
192 Raises:
193 IndexError: If the stack is empty.
194 """
195 if self.is_empty():
196 raise IndexError("Stack is empty")
197 return self.head.value

Callers 1

Calls 1

is_emptyMethod · 0.45

Tested by 1