Algorithms and Design Patterns

Python implementations of data structures, algorithms and design patterns. Also see awesome-algorithms.

If a Python algorithms library has to run in production, Sorted Containers gives you sorted lists, dicts, and sets. To learn, read The Algorithms - Python.

How to choose:

Listed in editorial order, grouped by use case. Click a column header to re-sort the whole list.

Press / to search. Tags open their category. Click a row for details.

Search and filter

Results

Row number Tags

Algorithms 3 projects

Fast and pure-Python implementation of sorted collections.
Minimal examples of data structures and algorithms.
keon/github.com/keon/algorithms /25,584 stars /
All algorithms implemented in Python.
TheAlgorithms/github.com/TheAlgorithms/Python /225,681 stars /

Design Patterns 2 projects

A collection of design patterns and idioms in Python.
faif/github.com/faif/python-patterns /43,078 stars /
Expressive statecharts and finite state machines with a declarative API, in sync and async codebases.

Algorithms and Design Patterns guide

Python's standard library is great until you need a sorted collections type, and Sorted Containers fills that gap in pure Python, as fast as C extensions. SortedList keeps its values in order as you add() them, SortedDict is a dict that keeps a sorted list of its keys, and SortedSet does the same for a set.

algorithms collects minimal, clean, and well-documented implementations of data structures and algorithms. Each file stands alone, with docstrings, type hints, and complexity notes, and its README says they're designed to be read and learned from. Install it with pip install algorithms, then import an implementation by topic, like a sort or a graph search.

The Algorithms - Python implements algorithms for education, and its README says they may be less efficient than the standard library's own: read them to learn, not to ship. Start from its directory, which sorts them by topic. Its contributing guide asks each algorithm to carry type hints and doctests that test both valid and erroneous input, so the doctests show you how it behaves.

python-patterns is a collection of design patterns and idioms in Python, one file per pattern, grouped as creational, structural, behavioral, and more. Its README asks you to pay more attention to why you choose a pattern than to how you implement it. It also lists the patterns not recommended in Python: skip a Singleton class, since modules are already singletons, and prefer composition and delegation over deep inheritance.

python-statemachine builds flat state machines or full statecharts with compound states, parallel regions, and history, in sync and async code. Declare states as class attributes and transitions with state.to(target), and the events are the names you assign them to.

Before you hand-write python-patterns' State pattern for a real workflow, read python-statemachine's guide for coming from the State pattern. The whole workflow sits in one class body, and unreachable states fail when the class is defined, not at runtime. For more places to learn and practice algorithms, see awesome-algorithms.

Know a project that belongs here?

Tell us what it does and why it stands out.