All 40 snippets
Every example on every page is a doctest, run on Python 3.11–3.14.
pysnippets.lists
16 modules- bifurcate_by(items, predicate)Partition items into those that pass a test and those that don't, in one pass.
- chunk_into_n(items, n)Split a sequence into exactly n lists whose sizes differ by at most one.
- count_by(items, key)Count how many items map to each key, using collections.Counter.
- deep_flatten(items)Lazily yield every value from arbitrarily nested iterables, treating strings as values.
- difference_by(a, b, key)Keep the items of one list whose key doesn't appear in another, preserving order.
- duplicates(items) +1List the values that repeat, or the ones that appear exactly once.
- every_nth(items, n)Take every nth item from a sequence, starting with the nth, using a slice.
- group_by(items, key)Bucket items into lists keyed by a function, keeping the original order inside each group.
- has_duplicates(items)Return True as soon as any value repeats, even in a huge or endless iterable.
- have_same_contents(a, b)Compare two collections regardless of order, counting duplicates, in linear time.
- index_of_all(items, value) +1Return all positions of a value, or of items matching a predicate.
- intersection_by(a, b, key)Keep the items of one list whose key also appears in another, preserving order.
- most_frequent(items)Return the most common item, breaking ties by first appearance.
- sort_by_indexes(items, indexes, *, reverse)Order items by the number at the same position in a second list.
- union_by(a, b, key)Merge two lists, keeping the first item for each key, in a predictable order.
- unique_elements(items)De-duplicate a list while keeping the first occurrence of each value, in order.
pysnippets.dicts
8 modules- collect_dictionary(d)Invert a dict so each value maps to the list of keys that had it.
- find_keys(d, value)Return every key whose value equals a given value.
- get_nested(data, path, default)Follow a path of keys and indexes through nested dicts and lists, with a default.
- invert_dictionary(d)Swap keys and values with a dict comprehension; the last key wins on shared values.
- key_of_max(d) +1Find the key with the largest or smallest value using max() or min() with a key function.
- map_values(d, fn)Build a new dict with a function applied to each value, keeping the keys.
- pluck(records, key)Collect the value of a key from each dict, with None where it's missing.
- sort_dict_by_value(d, *, reverse)Return a copy of a dict ordered by its values, ascending or descending.
pysnippets.strings
8 modules- byte_size(text, encoding)Count the bytes a string takes up once encoded, which len() doesn't tell you.
- hamming_distance(a, b)Count the positions at which two equal-length strings differ.
- hex_to_rgb(color) +1Parse #RRGGBB and #RGB colours into (r, g, b), and format them back.
- is_anagram(a, b)Compare letter counts, ignoring case, spaces and punctuation.
- is_palindrome(text)Check whether text reads the same reversed, ignoring case, spaces and punctuation.
- split_words(text) +4Convert text between snake_case, kebab-case, camelCase and PascalCase, acronyms included.
- slugify(text)Make a lowercase, ASCII, hyphen-separated slug from any text, including accented letters.
- words(text, pattern)Extract the words from text with a regex, keeping apostrophes and hyphens inside words.
pysnippets.numbers
5 modules- clamp_number(value, low, high)Limit a value to an inclusive range, accepting the bounds in either order.
- digitize(n)Turn an integer into a list of its decimal digits.
- is_prime(n)Test primality by trial division up to the integer square root.
- num_to_range(value, in_min, in_max, out_min, out_max)Linearly map a value from one range onto another, like Arduino's map().
- to_roman_numeral(number)Convert 1 to 3999 into Roman numerals with a greedy table, rejecting values outside that range.