FIND YOUR NEXT FEW LINES

Results for “Python”

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

125 snippets foundClear filters
PythonMappings & sets

Group records by a field

Python: Accumulate related values with defaultdict without checking whether each group exists.

Python✓ Tested
PythonFunctions & error handling

Handle an expected child-process failure

Python: Catch CalledProcessError when check=True reports a nonzero exit status.

Python✓ Tested
PythonStructured data & serialization

Handle malformed JSON input

Python: Catch JSONDecodeError without hiding unrelated programming errors.

Python✓ Tested
PythonGetting Started

Handle three possible outcomes

Python: Check high, normal and low values with an ordered conditional chain.

Python✓ Tested
PythonFile operations & archives

Hash a file without reading it all at once

Python: Feed fixed-size binary chunks into SHA-256 for files larger than memory.

Python✓ Tested
PythonNumbers & calculations

Insert a value while preserving sorted order

Python: Use insort on an already sorted list; insertion still shifts later list elements.

Python✓ Tested
PythonMappings & sets

Invert a dictionary with unique values

Python: Swap keys and values when the values are hashable and unique.

Python✓ 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
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
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