Tags

Python Standard Library

Python Standard Library

0 lessons
25 code snippets
1 community item

py-standard-library

Code Snippets

25 snippets
Code Snippet

Walrus Operator Patterns

The walrus operator `:=` (Python 3.8+) lets you assign and use a value in the same expression. It is the right tool for caching expensive expressions inside comprehensions, reading until a sentinel, and tightening the common 'compute, then check' pattern. This snippet covers the loop-with-sentinel use, the comprehension-cache use, and the regex-match conditional that is the most-cited textbook example.

Python
py-walrus-operator
code-template
cheat-sheet
py-standard-library

605

11

Medium
Code Snippet

Counter for Frequency Counting

`collections.Counter` is the dict-of-counts that every other 'count occurrences' implementation tries to be. It supports increment-by-add, most-common-K, and arithmetic between counters. This snippet covers the basic frequency count, the most-common-K shortcut, and the multiset arithmetic that makes Counter the right choice for inventory math and difference reports.

Python
py-collections
py-standard-library
code-template
hash-table

1.1k

25

Easy
Code Snippet

defaultdict for Implicit Init

`collections.defaultdict` removes the boilerplate of checking-then-initialising before every increment or append. It supplies a default value when a missing key is read, and that default lives in the dict from then on. This snippet covers the bucket-by-key pattern with `defaultdict(list)`, the count pattern with `defaultdict(int)`, and a nested `defaultdict` for two-level groupings.

Python
py-collections
py-standard-library
code-template
hash-table

348

2

Easy
Code Snippet

deque for O(1) Append and Pop on Both Ends

A `collections.deque` (double-ended queue) supports O(1) `append`, `appendleft`, `pop`, and `popleft`, while a Python `list` is O(n) for left-side operations. This snippet covers the deque-as-queue pattern that powers BFS, the deque-as-rolling-buffer pattern with `maxlen`, and the rotate trick for cyclic processing.

Python
py-collections
py-standard-library
code-template
queue

920

9

Easy
Code Snippet

namedtuple for Lightweight Records

`collections.namedtuple` produces a tuple subclass with named fields, giving you the immutability and packing of a tuple plus the readability of a dataclass. It is the right choice for tiny return-record types that should be cheap and hashable. This snippet covers the basic factory, the typed `NamedTuple` form from `typing`, and the `_replace` and `_asdict` helpers for record-style updates and JSON conversion.

Python
py-collections
py-standard-library
code-template
py-dataclasses

1k

11

Easy
Code Snippet

OrderedDict Quirks Worth Knowing

Regular dicts have preserved insertion order since Python 3.7, so most modern code never reaches for `OrderedDict`. But OrderedDict still has a niche: it ships with `move_to_end` and `popitem(last=False)` methods that plain dicts do not, and its equality semantics differ from dict equality. This snippet covers the move-to-end LRU primitive, the order-sensitive equality, and when you should still pick OrderedDict in 2025.

Python
py-collections
py-standard-library
code-template
lru-cache

577

5

Medium
Code Snippet

ChainMap for Layered Configs

`collections.ChainMap` lets you stack multiple dicts and treat them as a single read-through view, with later dicts shadowing earlier ones. It is the right primitive for layered configs (defaults, environment, user overrides), nested scopes, and anywhere you would otherwise merge dicts repeatedly. This snippet covers the basic stacking, lookup-with-fallback semantics, and how new keys land in the first map by default.

Python
py-collections
py-standard-library
code-template
cheat-sheet

451

10

Medium
Code Snippet

heapq Min-Heap Recipes

Python's `heapq` module turns any list into a binary min-heap in place, supporting O(log n) push and pop. It is the priority-queue primitive that powers Dijkstra, Top-K, and merging sorted streams. This snippet covers the basic push and pop, the Top-K largest pattern using `nsmallest` and `nlargest`, and the merge of multiple sorted iterables in O(N log K).

Python
py-standard-library
heap
code-template
algorithms

586

17

Medium
Code Snippet

bisect for Sorted-List Insertion

The `bisect` module is Python's binary-search-on-a-list primitive: `bisect_left` and `bisect_right` find the insertion index for a value in a sorted list in O(log n). This snippet covers the basic insertion-point query, the `insort` shortcut for keeping a list sorted as you build it, and the count-occurrences and rank-percentile recipes that fall out for free.

Python
py-standard-library
binary-search
code-template
algorithms

189

2

Medium
Code Snippet

Flatten with itertools.chain

`itertools.chain` lazily concatenates several iterables into a single one without copying their elements, which is the right tool for flattening a list of lists by exactly one level. It works on any iterable (lists, tuples, generators, file objects), so it composes cleanly with the rest of the iterator toolbox. This entry covers `chain`, the unpacking-friendly `chain.from_iterable`, and how it differs from a recursive deep flatten.

Python
py-itertools
py-generators
iterators
py-standard-library

958

15

Easy
Code Snippet

Combinations and Permutations

When the problem reads 'pick K of N' or 'order all N', the right reflex in Python is `itertools.combinations` or `itertools.permutations`. Both are lazy iterators, so they enumerate huge search spaces without materializing them. This entry walks combinations, permutations, and `combinations_with_replacement`, plus when each is the right tool.

Python
py-itertools
py-generators
iterators
py-standard-library

235

2

Easy
Code Snippet

Group Consecutive Items with groupby

`itertools.groupby` collapses runs of equal-keyed items into `(key, group_iterator)` pairs. The catch is that it only groups *consecutive* equal items, so the input must already be sorted by the key if you want full grouping. This snippet covers run-length encoding, the sort-first idiom for dict-like grouping, and the iterator gotcha that bites every newcomer.

Python
py-itertools
py-generators
iterators
py-standard-library

768

5

Easy
Code Snippet

Build a Generator Pipeline

A generator pipeline chains small `yield`-based stages so data flows through them one item at a time. The result is constant-memory streaming over inputs that would not fit in RAM, with each stage doing one job (read, parse, filter, transform, sink). This entry shows a three-stage pipeline, how to compose stages dynamically, and why generator pipelines beat list-of-lists processing for log-style data.

Python
py-generators
iterators
py-itertools
py-standard-library

209

5

Medium
Code Snippet

Custom Context Manager (Class-Based)

A class-based context manager defines `__enter__` and `__exit__` so the object can be used in a `with` block. It is the right shape when the resource has setup, teardown, AND state you want to expose to the body (file handles, DB connections, locks). This entry shows the basic skeleton, an exception-aware variant, and the gotchas around return values from `__exit__`.

Python
py-context-managers
py-magic-methods
py-standard-library

216

5

Medium
Code Snippet

Custom Context Manager via contextlib

`@contextlib.contextmanager` turns a generator into a context manager: code before the `yield` is the setup, the yielded value is bound by `as`, and code after the `yield` is the teardown. It removes most of the class boilerplate when you do not need shared state. This entry covers the basic pattern, exception handling with try/finally, and `contextlib.suppress` as a one-liner.

Python
py-context-managers
py-decorators
py-standard-library

967

17

Medium
Code Snippet

Read and Write JSON Files Idiomatically

Reading and writing JSON in Python is two lines once you know the right defaults. The right defaults are `with open(... encoding='utf-8')`, `json.load` / `json.dump` (not `loads` / `dumps`), and `indent=2, ensure_ascii=False` for human-readable files. This entry covers the round trip, atomic write, and the common pitfalls (bytes vs text mode, default ASCII escaping).

Python
file-io
py-standard-library
code-template

772

10

Medium
Code Snippet

dataclass Basics

`@dataclass` writes the boring boilerplate for you: `__init__`, `__repr__`, and `__eq__`. You declare fields with type hints and (optionally) defaults, and Python builds the constructor and the value-equality semantics. This entry covers the basic shape, default values done right, and the `field()` escape hatch for mutable defaults.

Python
py-dataclasses
py-type-hints
py-standard-library

530

5

Easy
Code Snippet

Optional, Union, Literal Type Hints

Modern Python type hints (3.10+) replaced most of `typing.Optional` and `typing.Union` with the `X | Y` operator and made `Literal` the right tool for closed string sets. This entry covers `T | None` for nullable values, `X | Y` for unions, and `Literal['a', 'b']` for enum-like APIs, with runtime examples that show why each shape matters.

Python
py-type-hints
py-standard-library

960

11

Easy
Code Snippet

Frozen + slots Dataclasses

`@dataclass(frozen=True)` makes instances immutable and hashable, which lets you use them as dict keys or set members. `slots=True` (3.10+) skips the `__dict__` allocation, saving roughly 40% of memory per instance and making attribute access slightly faster. This entry covers each flag separately and shows when combining them is the right call.

Python
py-dataclasses
py-slots
py-type-hints
py-standard-library

591

13

Medium
Code Snippet

Structural Typing with Protocol

`typing.Protocol` adds static structural typing (duck typing) to Python: any object that has the right methods is acceptable, no inheritance required. It is the right tool for plugin interfaces, dependency injection, and 'looks like a file' style APIs. This entry covers a basic protocol, the `@runtime_checkable` switch, and how Protocol differs from an abstract base class.

Python
py-type-hints
py-standard-library

1.1k

25

Medium
Code Snippet

Python Two-Pointer Template

The two-pointer pattern walks two indices through a sorted array, moving them inward (or together) based on a comparison. It turns many naive O(n^2) problems into O(n) sweeps. This entry covers the inward-sweep template (two-sum sorted), the same-direction template (remove duplicates in place), and a 3-sum builder that uses two pointers as the inner loop.

Python
two-pointers
algorithms
code-template
py-standard-library

930

4

Easy
Code Snippet

Python Sliding Window Template

The sliding-window pattern walks two indices forward through a sequence, maintaining an aggregate (sum, count, set, dict) of the current window. It turns 'best subarray of length K' and 'longest subarray with property P' problems into single O(n) sweeps. This entry covers the fixed-size variant, the variable-size shrink-when-invalid variant, and the longest-substring-without-repeats classic.

Python
sliding-window
algorithms
code-template
py-standard-library

1.1k

5

Easy
Code Snippet

Graph Adjacency List in Python

An adjacency list represents a graph as 'for each node, the nodes it connects to'. In Python the right shape is `defaultdict(list)`: insertion is one line, traversal is one nested loop, and you do not pay for the V*V matrix. This entry covers building the list, walking it with DFS, and the directed vs undirected detail that bites every newcomer.

Python
graphs
graph-representation
data-structures
py-standard-library

736

9

Medium
Code Snippet

Trie Implementation in Python

A trie (prefix tree) stores strings character-by-character so prefix queries run in O(length) instead of O(N * length). The Pythonic shape uses a dict of dicts plus a sentinel key for 'word ends here'. This entry covers the dict-of-dicts trie, a class-based variant with explicit nodes, and the autocomplete query you build it for.

Python
trie
trie-operations
data-structures
py-standard-library

697

21

Medium
Code Snippet

LRU Cache via OrderedDict

An LRU (least-recently-used) cache evicts whichever entry has been untouched the longest when it hits its capacity. `collections.OrderedDict` makes the implementation tiny: `move_to_end` keeps the most-recently-used key at the back, and `popitem(last=False)` evicts the front. This entry covers the get/put loop, the `@functools.lru_cache` shortcut, and a mini benchmark.

Python
lru-cache
data-structures
py-collections
py-standard-library

219

5

Medium