FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

125 snippets foundClear filters
PythonStrings & dates

Join strings with a separator

Combine a list of strings into one readable string with a separator between each item.

string✓ Tested
PythonMappings & sets

Keep only the most recent items

Python: Use a bounded deque to discard the oldest item as new values arrive.

Python✓ Tested
PythonFiles & paths

List files in a folder

List the names of regular files in a folder in alphabetical order, excluding subdirectories.

directory✓ Tested
PythonGetting Started

Measure a short text value

Python: Read the length of an ASCII string. Non-ASCII text may use multiple code units or bytes depending on the language.

Python✓ Tested
PythonCollections

Merge two dictionaries

Combine two dictionaries into a new dictionary. When a key appears in both inputs, the value from the second dictionary wins.

dictionary✓ Tested
PythonGetting Started

Name a value that stays fixed

Python: Use a constant declaration or the language’s constant naming convention for a fixed limit.

Python✓ Tested
PythonText validation & encoding

Normalize equivalent Unicode strings

Python: Normalize to NFC before comparing canonically equivalent text.

Python✓ Tested
PythonIteration & sequences

Number items while iterating

Python: Use enumerate to attach a one-based position without maintaining a separate counter.

Python✓ Tested
PythonStructured data & serialization

Pack and unpack a network-order integer

Python: Use an explicit byte order so the binary format is independent of the host architecture.

Python✓ Tested
PythonIteration & sequences

Pair corresponding list items

Python: Zip two equally sized sequences into pairs; strict mode rejects mismatched lengths on Python 3.10 or later.

Python✓ Tested
PythonJSON & CSV

Parse a JSON string

Convert a JSON string into a Python dictionary and read values by key.

json✓ Tested
PythonFunctions & error handling

Parse a typed command-line option

Python: Use argparse to validate and convert an integer option.

Python✓ Tested