Community Python Snippet

A Production Graph Adjacency List With Node Metadata

The bare adjacency list in the official catalog is a starting point. In production I need node metadata, safe edge deletion, and a traversal helper that does not break when a node is removed mid-walk.

A Production Graph Adjacency List With Node Metadata

The bare adjacency list in the official catalog is a starting point. In production I need node metadata, safe edge deletion, and a traversal helper that does not break when a node is removed mid-walk.

Python
Compiler
3 snippets
adjacency-list
graph-representation
py-collections
kavyachakraborty

By @kavyachakraborty

December 20, 2025

·

Updated May 20, 2026

706 views

5

4.2 (10)

Two stores, one purpose: nodes holds the metadata dict per id, and edges holds a src -> {dst: rel} map so each edge has a typed relationship. The cost of the metadata dict is one extra hash lookup per node access, which is negligible compared to having to denormalize node data into every edge. The add_edge checks both endpoints; that single line has caught dozens of typos in production. The neighbors method returns a list (not the underlying dict view) so the caller can safely mutate the graph during iteration, which the next accordion needs.