MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / popleft

Method popleft

data_structures/queues/double_ended_queue.py:290–334  ·  view source on GitHub ↗

Removes the first element of the deque and returns it. Time complexity: O(1) @returns topop.val: the value of the node to pop. >>> our_deque1 = Deque([1]) >>> our_popped1 = our_deque1.pop() >>> our_popped1 1 >>> our_deque1 []

(self)

Source from the content-addressed store, hash-verified

288 return topop.val
289
290 def popleft(self) -> Any:
291 """
292 Removes the first element of the deque and returns it.
293 Time complexity: O(1)
294 @returns topop.val: the value of the node to pop.
295 >>> our_deque1 = Deque([1])
296 >>> our_popped1 = our_deque1.pop()
297 >>> our_popped1
298 1
299 >>> our_deque1
300 []
301 >>> our_deque2 = Deque([15182, 1, 2, 3])
302 >>> our_popped2 = our_deque2.popleft()
303 >>> our_popped2
304 15182
305 >>> our_deque2
306 [1, 2, 3]
307 >>> from collections import deque
308 >>> deque_collections = deque([15182, 1, 2, 3])
309 >>> collections_popped = deque_collections.popleft()
310 >>> collections_popped
311 15182
312 >>> deque_collections
313 deque([1, 2, 3])
314 >>> list(our_deque2) == list(deque_collections)
315 True
316 >>> our_popped2 == collections_popped
317 True
318 """
319 # make sure the deque has elements to pop
320 assert not self.is_empty(), "Deque is empty."
321
322 topop = self._front
323 # if only one element in the queue: point the front and back to None
324 # else remove one element from front
325 if self._front == self._back:
326 self._front = None
327 self._back = None
328 else:
329 self._front = self._front.next_node # set new front and drop the first node
330 self._front.prev_node = None
331
332 self._len -= 1
333
334 return topop.val
335
336 def is_empty(self) -> bool:
337 """

Callers 15

set_fail_transitionsMethod · 0.80
round_robinMethod · 0.80
bfsFunction · 0.80
topoFunction · 0.80
is_bipartite_bfsFunction · 0.80
bfs_shortest_pathFunction · 0.80
get_shortest_pathMethod · 0.80
bfsMethod · 0.80
bfsMethod · 0.80
expand_searchFunction · 0.80

Calls 1

is_emptyMethod · 0.95

Tested by 3

bfs_shortest_pathFunction · 0.64
get_shortest_pathMethod · 0.64