Skip to content

Instantly share code, notes, and snippets.

@jonathanagustin
Last active April 30, 2020 06:18
Show Gist options
  • Select an option

  • Save jonathanagustin/123925e97ce7549d5f02a6d32be5e692 to your computer and use it in GitHub Desktop.

Select an option

Save jonathanagustin/123925e97ce7549d5f02a6d32be5e692 to your computer and use it in GitHub Desktop.
Python Queue vs Deque - pop() vs popleft()

Tip: To make the code more efficient, you can use the deque object from the collections module instead of a list, for implementing queue. This way you can use the popleft() method instead of the pop(0) built-in function on queue. This will result in a quicker code as popleft() has a time complexity of O(1) while pop(0) has O(n).

Deque (Doubly Ended Queue) in python is implemented using the module “collections“. Deque is preferred over list in the cases where we need quicker append and pop operations from both the ends of container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment