Skip to content

Archive

Algorithms

3 articles
Python 02 Sep 2026 9 min read

Practical Queues and Sliding Windows with Python deque

Many programs need a sequence that changes at both ends. A worker may append new jobs on the right and consume the oldest job from the left. A monitoring loop may keep only the most recent measurements. An algorithm may need to add or remove candidates from either side while scanning an input stream. A Python list is excellent when random access and operations near the right end dominate. It is a poor fit for a FIFO queue that repeatedly removes index zero, because the remaining list elements must be shifted. The collections.deque type is designed for efficient appends and pops at both ends.

Python 02 Sep 2026 10 min read

Practical Priority Queues in Python with heapq

Many programs need to repeatedly choose the most important pending item rather than process items in insertion order. Schedulers pick the next deadline, graph algorithms choose the lowest-cost candidate, and streaming systems keep only the best few observations seen so far. A sorted list can solve these problems, but maintaining full ordering is often unnecessary. Python’s heapq module provides a heap: a compact data structure that keeps one extreme element immediately available while doing only enough work to preserve that property.

Python 02 Sep 2026 8 min read

Maintain Sorted Sequences in Python with bisect

A sorted list is useful when a program needs ordered iteration and frequent searches but does not require the repeated minimum extraction of a priority queue. Python’s bisect module provides binary-search operations for this exact representation. The module does not create a special container. It works with an existing sorted sequence and finds the position where a value belongs. That makes it small and predictable, but it also means the caller is responsible for preserving sorted order and understanding that inserting into a Python list still requires moving elements.