Loading video player…

Working With Python's deque (Overview)

You can use Python’s deque for efficient appends and pops at both ends of a sequence-like data type. These capabilities are critical when you need to implement queue and stack data structures that operate efficiently even under heavy workloads.

In this video course, you’ll learn how deque works, when to use it over a list, and how to apply it in real code.

By the end of this video course, you’ll understand that:

  • deque internally uses a doubly linked list, so end operations are O(1) while random indexing is O(n).
  • You can build a FIFO queue with .append() and .popleft(), and a LIFO stack with .append() and .pop().
  • deque supports indexing but doesn’t support slicing.
  • Passing a value to maxlen creates a bounded deque that drops items from the opposite end when full.
  • In CPython, .append(), .appendleft(), .pop(), .popleft(), and len() are thread-safe for multithreaded use.

Up next, you’ll get started with deque, benchmark it against list, and explore how it shines in real-world use cases, such as queues, stacks, history buffers, and thread-safe producer-consumer setups.

Download

Course Slides (.pdf)

7.6 MB
Download

Sample Code (.zip)

4.7 KB

Become a Member to join the conversation.