# Python snippets

> Small Python functions for lists, dictionaries, strings, maths and dates, each with a short explanation and an example call.

Canonical: https://www.wiki.jodisand.me/snippets/python/
Reviewed: 2026-09-24
Related: [Python](https://www.wiki.jodisand.me/python/index.md), [Testing](https://www.wiki.jodisand.me/testing/index.md)


Short third-party examples with no input validation or error handling, and most have no type hints. Check the standard library first (`itertools`, `functools`, `collections`), since many have a built-in equivalent. See [Python](https://www.wiki.jodisand.me/python/) for project and tooling guidance.


## Pages

- [Add days to date](https://www.wiki.jodisand.me/snippets/python/add_days/index.md): Calculates the date of n days from the given date.
- [All indexes of value](https://www.wiki.jodisand.me/snippets/python/index_of_all/index.md): Returns a list of indexes of all the occurrences of an element in a list.
- [Apply function when true](https://www.wiki.jodisand.me/snippets/python/when/index.md): Tests a value, x, against a testing function, conditionally applying a function.
- [Arithmetic progression](https://www.wiki.jodisand.me/snippets/python/arithmetic_progression/index.md): Generates a list of numbers in the arithmetic progression starting with the given positive integer and up to the specified limit.
- [Average](https://www.wiki.jodisand.me/snippets/python/average/index.md): Calculates the average of two or more numbers.
- [Bifurcate list based on function](https://www.wiki.jodisand.me/snippets/python/bifurcate_by/index.md): Splits values into two groups, based on the result of the given filtering function.
- [Bifurcate list based on values](https://www.wiki.jodisand.me/snippets/python/bifurcate/index.md): Splits values into two groups, based on the result of the given filter list.
- [Binomial coefficient](https://www.wiki.jodisand.me/snippets/python/binomial_coefficient/index.md): Calculates the number of ways to choose k items from n items without repetition and without order.
- [Byte size of string](https://www.wiki.jodisand.me/snippets/python/byte_size/index.md): Returns the length of a string in bytes.
- [Camelcase string](https://www.wiki.jodisand.me/snippets/python/camel/index.md): Converts a string to camelcase.
- [Capitalize every word](https://www.wiki.jodisand.me/snippets/python/capitalize_every_word/index.md): Capitalizes the first letter of every word in a string.
- [Capitalize string](https://www.wiki.jodisand.me/snippets/python/capitalize/index.md): Capitalizes the first letter of a string.
- [Cast to list](https://www.wiki.jodisand.me/snippets/python/cast_list/index.md): Casts the provided value as a list if it’s not one.
- [Celsius to Fahrenheit](https://www.wiki.jodisand.me/snippets/python/celsius_to_fahrenheit/index.md): Converts Celsius to Fahrenheit.
- [Check for duplicates in list](https://www.wiki.jodisand.me/snippets/python/has_duplicates/index.md): Checks if there are duplicate values in a flat list.
- [Check if list elements are identical](https://www.wiki.jodisand.me/snippets/python/all_equal/index.md): Checks if all elements in a list are equal.
- [Check if list has no duplicates](https://www.wiki.jodisand.me/snippets/python/all_unique/index.md): Checks if all the values in a list are unique.
- [Check lists have same contents](https://www.wiki.jodisand.me/snippets/python/have_same_contents/index.md): Checks if two lists contain the same elements regardless of order.
- [Check property](https://www.wiki.jodisand.me/snippets/python/check_prop/index.md): Creates a function that will invoke a predicate function for the specified property on a given dictionary.
- [Clamp number](https://www.wiki.jodisand.me/snippets/python/clamp_number/index.md): Clamps num within the inclusive range specified by the boundary values.
- [Combine dictionary values](https://www.wiki.jodisand.me/snippets/python/combine_values/index.md): Combines two or more dictionaries, creating a list of values for each key.
- [Compact list](https://www.wiki.jodisand.me/snippets/python/compact/index.md): Removes falsy values from a list.
- [Compose functions](https://www.wiki.jodisand.me/snippets/python/compose/index.md): Performs right-to-left function composition.
- [Count grouped elements](https://www.wiki.jodisand.me/snippets/python/count_by/index.md): Groups the elements of a list based on the given function and returns the count of elements in each group.
- [Count occurrences](https://www.wiki.jodisand.me/snippets/python/count_occurrences/index.md): Counts the occurrences of a value in a list.
- [Curry function](https://www.wiki.jodisand.me/snippets/python/curry/index.md): Curries a function.
- [Date difference in days](https://www.wiki.jodisand.me/snippets/python/days_diff/index.md): Calculates the day difference between two dates.
- [Date difference in months](https://www.wiki.jodisand.me/snippets/python/months_diff/index.md): Calculates the month difference between two dates.
- [Date from ISO format](https://www.wiki.jodisand.me/snippets/python/from_iso_date/index.md): Converts a date from its ISO-8601 representation.
- [Date is weekday](https://www.wiki.jodisand.me/snippets/python/is_weekday/index.md): Checks if the given date is a weekday.
- [Date is weekend](https://www.wiki.jodisand.me/snippets/python/is_weekend/index.md): Checks if the given date is a weekend.
- [Date range](https://www.wiki.jodisand.me/snippets/python/daterange/index.md): Creates a list of dates between start (inclusive) and end (not inclusive).
- [Date to ISO format](https://www.wiki.jodisand.me/snippets/python/to_iso_date/index.md): Converts a date to its ISO-8601 representation.
- [Days ago](https://www.wiki.jodisand.me/snippets/python/days_ago/index.md): Calculates the date of n days ago from today.
- [Days from now](https://www.wiki.jodisand.me/snippets/python/days_from_now/index.md): Calculates the date of n days from today.
- [Decapitalize string](https://www.wiki.jodisand.me/snippets/python/decapitalize/index.md): Decapitalizes the first letter of a string.
- [Deep flatten list](https://www.wiki.jodisand.me/snippets/python/deep_flatten/index.md): Deep flattens a list.
- [Degrees to radians](https://www.wiki.jodisand.me/snippets/python/degrees_to_rads/index.md): Converts an angle from degrees to radians.
- [Delayed function execution](https://www.wiki.jodisand.me/snippets/python/delay/index.md): Invokes the provided function after ms milliseconds.
- [Dictionary keys](https://www.wiki.jodisand.me/snippets/python/keys_only/index.md): Creates a flat list of all the keys in a flat dictionary.
- [Dictionary to list](https://www.wiki.jodisand.me/snippets/python/dict_to_list/index.md): Converts a dictionary to a list of tuples.
- [Dictionary values](https://www.wiki.jodisand.me/snippets/python/values_only/index.md): Returns a flat list of all the values in a flat dictionary.
- [Digitize number](https://www.wiki.jodisand.me/snippets/python/digitize/index.md): Converts a number to a list of digits.
- [Drop list elements from the left](https://www.wiki.jodisand.me/snippets/python/drop/index.md): Returns a list with n elements removed from the left.
- [Drop list elements from the right](https://www.wiki.jodisand.me/snippets/python/drop_right/index.md): Returns a list with n elements removed from the right.
- [Every nth element in list](https://www.wiki.jodisand.me/snippets/python/every_nth/index.md): Returns every nth element in a list.
- [Execute function for each list element](https://www.wiki.jodisand.me/snippets/python/for_each/index.md): Executes the provided function once for each list element.
- [Execute function for each list element in reverse](https://www.wiki.jodisand.me/snippets/python/for_each_right/index.md): Executes the provided function once for each list element, starting from the list’s last element.
- [Factorial](https://www.wiki.jodisand.me/snippets/python/factorial/index.md): Calculates the factorial of a number.
- [Fahrenheit to Celsius](https://www.wiki.jodisand.me/snippets/python/fahrenheit_to_celsius/index.md): Converts Fahrenheit to Celsius.
- [Fibonacci](https://www.wiki.jodisand.me/snippets/python/fibonacci/index.md): Generates a list, containing the Fibonacci sequence, up until the nth term.
- [Filter non-unique list values](https://www.wiki.jodisand.me/snippets/python/filter_non_unique/index.md): Creates a list with the non-unique values filtered out.
- [Filter unique list values](https://www.wiki.jodisand.me/snippets/python/filter_unique/index.md): Creates a list with the unique values filtered out.
- [Find all matching indexes](https://www.wiki.jodisand.me/snippets/python/find_index_of_all/index.md): Finds the indexes of all elements in the given list that satisfy the provided testing function.
- [Find key of value](https://www.wiki.jodisand.me/snippets/python/find_key/index.md): Finds the first key in the provided dictionary that has the given value.
- [Find keys with value](https://www.wiki.jodisand.me/snippets/python/find_keys/index.md): Finds all keys in the provided dictionary that have the given value.
- [Find last matching index](https://www.wiki.jodisand.me/snippets/python/find_last_index/index.md): Finds the index of the last element in the given list that satisfies the provided testing function.
- [Find last matching value](https://www.wiki.jodisand.me/snippets/python/find_last/index.md): Finds the value of the last element in the given list that satisfies the provided testing function.
- [Find matching index](https://www.wiki.jodisand.me/snippets/python/find_index/index.md): Finds the index of the first element in the given list that satisfies the provided testing function.
- [Find matching value](https://www.wiki.jodisand.me/snippets/python/find/index.md): Finds the value of the first element in the given list that satisfies the provided testing function.
- [Find parity outliers](https://www.wiki.jodisand.me/snippets/python/find_parity_outliers/index.md): Finds the items that are parity outliers in a given list.
- [Flatten list](https://www.wiki.jodisand.me/snippets/python/flatten/index.md): Flattens a list of lists once.
- [Geometric progression](https://www.wiki.jodisand.me/snippets/python/geometric_progression/index.md): Initializes a list containing the numbers in the specified range where start and end are inclusive and the ratio between two terms is step.
- [Get nested value](https://www.wiki.jodisand.me/snippets/python/get/index.md): Retrieves the value of the nested key indicated by the given selector list from a dictionary or list.
- [Greatest common divisor](https://www.wiki.jodisand.me/snippets/python/gcd/index.md): Calculates the greatest common divisor of a list of numbers.
- [Group list elements](https://www.wiki.jodisand.me/snippets/python/group_by/index.md): Groups the elements of a list based on the given function.
- [Hamming distance](https://www.wiki.jodisand.me/snippets/python/hamming_distance/index.md): Calculates the Hamming distance between two values.
- [Hex to RGB](https://www.wiki.jodisand.me/snippets/python/hex_to_rgb/index.md): Converts a hexadecimal color code to a tuple of integers corresponding to its RGB components.
- [Index of max element](https://www.wiki.jodisand.me/snippets/python/max_element_index/index.md): Returns the index of the element with the maximum value in a list.
- [Index of min element](https://www.wiki.jodisand.me/snippets/python/min_element_index/index.md): Returns the index of the element with the minimum value in a list.
- [Initialize 2D list](https://www.wiki.jodisand.me/snippets/python/initialize_2d_list/index.md): Initializes a 2D list of given width and height and value.
- [Initialize list with range](https://www.wiki.jodisand.me/snippets/python/initialize_list_with_range/index.md): Initializes a list containing the numbers in the specified range where start and end are inclusive with their common difference step.
- [Initialize list with values](https://www.wiki.jodisand.me/snippets/python/initialize_list_with_values/index.md): Initializes and fills a list with the specified value.
- [Integer to roman numeral](https://www.wiki.jodisand.me/snippets/python/to_roman_numeral/index.md): Converts an integer to its roman numeral representation. Accepts value between 1 and 3999 (both inclusive).
- [Invert dictionary](https://www.wiki.jodisand.me/snippets/python/collect_dictionary/index.md): Inverts a dictionary with non-unique hashable values.
- [Invert dictionary](https://www.wiki.jodisand.me/snippets/python/invert_dictionary/index.md): Inverts a dictionary with unique hashable values.
- [Kebabcase string](https://www.wiki.jodisand.me/snippets/python/kebab/index.md): Converts a string to kebab case.
- [Key in dictionary](https://www.wiki.jodisand.me/snippets/python/key_in_dict/index.md): Checks if the given key exists in a dictionary.
- [Key of max value](https://www.wiki.jodisand.me/snippets/python/key_of_max/index.md): Finds the key of the maximum value in a dictionary.
- [Key of min value](https://www.wiki.jodisand.me/snippets/python/key_of_min/index.md): Finds the key of the minimum value in a dictionary.
- [Km to miles](https://www.wiki.jodisand.me/snippets/python/km_to_miles/index.md): Converts kilometers to miles.
- [Last list element](https://www.wiki.jodisand.me/snippets/python/last/index.md): Returns the last element in a list.
- [Least common multiple](https://www.wiki.jodisand.me/snippets/python/lcm/index.md): Returns the least common multiple of a list of numbers.
- [List difference](https://www.wiki.jodisand.me/snippets/python/difference/index.md): Calculates the difference between two iterables, without filtering duplicate values.
- [List difference based on function](https://www.wiki.jodisand.me/snippets/python/difference_by/index.md): Returns the difference between two lists, after applying the provided function to each list element of both.
- [List head](https://www.wiki.jodisand.me/snippets/python/head/index.md): Returns the head of a list.
- [List includes all values](https://www.wiki.jodisand.me/snippets/python/includes_all/index.md): Checks if all the elements in values are included in lst.
- [List includes any values](https://www.wiki.jodisand.me/snippets/python/includes_any/index.md): Checks if any element in values is included in lst.
- [List intersection](https://www.wiki.jodisand.me/snippets/python/intersection/index.md): Returns a list of elements that exist in both lists.
- [List intersection based on function](https://www.wiki.jodisand.me/snippets/python/intersection_by/index.md): Returns a list of elements that exist in both lists, after applying the provided function to each list element of both.
- [List is contained in other list](https://www.wiki.jodisand.me/snippets/python/is_contained_in/index.md): Checks if the elements of the first list are contained in the second one regardless of order.
- [List similarity](https://www.wiki.jodisand.me/snippets/python/similarity/index.md): Returns a list of elements that exist in both lists.
- [List symmetric difference](https://www.wiki.jodisand.me/snippets/python/symmetric_difference/index.md): Returns the symmetric difference between two iterables, without filtering out duplicate values.
- [List symmetric difference based on function](https://www.wiki.jodisand.me/snippets/python/symmetric_difference_by/index.md): Returns the symmetric difference between two lists, after applying the provided function to each list element of both.
- [List tail](https://www.wiki.jodisand.me/snippets/python/tail/index.md): Returns all elements in a list except for the first one.
- [List union](https://www.wiki.jodisand.me/snippets/python/union/index.md): Returns every element that exists in any of the two lists once.
- [List union based on function](https://www.wiki.jodisand.me/snippets/python/union_by/index.md): Returns every element that exists in any of the two lists once, after applying the provided function to each element of both.
- [List without last element](https://www.wiki.jodisand.me/snippets/python/initial/index.md): Returns all the elements of a list except the last one.
- [Lists to dictionary](https://www.wiki.jodisand.me/snippets/python/to_dictionary/index.md): Combines two lists into a dictionary, where the elements of the first one serve as the keys and the elements of the second one serve as the values. The values of the first list need to be unique and hashable.
- [Longest item](https://www.wiki.jodisand.me/snippets/python/longest_item/index.md): Takes any number of iterable objects or objects with a length property and returns the longest one.
- [Map dictionary values](https://www.wiki.jodisand.me/snippets/python/map_values/index.md): Creates a dictionary with the same keys as the provided dictionary and values generated by running the provided function for each value.
- [Map list to dictionary](https://www.wiki.jodisand.me/snippets/python/map_dictionary/index.md): Maps the values of a list to a dictionary using a function, where the key-value pairs consist of the original value as the key and the result of the function as the value.
- [Map number to range](https://www.wiki.jodisand.me/snippets/python/num_to_range/index.md): Maps a number from one range to another range.
- [Mapped list average](https://www.wiki.jodisand.me/snippets/python/average_by/index.md): Calculates the average of a list, after mapping each element to a value using the provided function.
- [Max list value based on function](https://www.wiki.jodisand.me/snippets/python/max_by/index.md): Returns the maximum value of a list, after mapping each element to a value using the provided function.
- [Median](https://www.wiki.jodisand.me/snippets/python/median/index.md): Finds the median of a list of numbers.
- [Merge dictionaries](https://www.wiki.jodisand.me/snippets/python/merge_dictionaries/index.md): Merges two or more dictionaries.
- [Merge lists](https://www.wiki.jodisand.me/snippets/python/merge/index.md): Merges two or more lists into a list of lists, combining elements from each of the input lists based on their positions.
- [Miles to km](https://www.wiki.jodisand.me/snippets/python/miles_to_km/index.md): Converts miles to kilometers.
- [Min list value based on function](https://www.wiki.jodisand.me/snippets/python/min_by/index.md): Returns the minimum value of a list, after mapping each element to a value using the provided function.
- [Most frequent element](https://www.wiki.jodisand.me/snippets/python/most_frequent/index.md): Returns the most frequent element in a list.
- [N max elements](https://www.wiki.jodisand.me/snippets/python/max_n/index.md): Returns the n maximum elements from the provided list.
- [N min elements](https://www.wiki.jodisand.me/snippets/python/min_n/index.md): Returns the n minimum elements from the provided list.
- [Number in range](https://www.wiki.jodisand.me/snippets/python/in_range/index.md): Checks if the given number falls within the given range.
- [Number is divisible](https://www.wiki.jodisand.me/snippets/python/is_divisible/index.md): Checks if the first numeric argument is divisible by the second one.
- [Number is even](https://www.wiki.jodisand.me/snippets/python/is_even/index.md): Checks if the given number is even.
- [Number is odd](https://www.wiki.jodisand.me/snippets/python/is_odd/index.md): Checks if the given number is odd.
- [Number is prime](https://www.wiki.jodisand.me/snippets/python/is_prime/index.md): Checks if the provided integer is a prime number.
- [Number to binary](https://www.wiki.jodisand.me/snippets/python/to_binary/index.md): Returns the binary representation of the given number.
- [Number to hex](https://www.wiki.jodisand.me/snippets/python/to_hex/index.md): Returns the hexadecimal representation of the given number.
- [Offset list elements](https://www.wiki.jodisand.me/snippets/python/offset/index.md): Moves the specified amount of elements to the end of the list.
- [Pad number](https://www.wiki.jodisand.me/snippets/python/pad_number/index.md): Pads a given number to the specified length.
- [Pad string](https://www.wiki.jodisand.me/snippets/python/pad/index.md): Pads a string on both sides with the specified character, if it’s shorter than the specified length.
- [Palindrome](https://www.wiki.jodisand.me/snippets/python/palindrome/index.md): Checks if the given string is a palindrome.
- [Partial sum list](https://www.wiki.jodisand.me/snippets/python/cumsum/index.md): Creates a list of partial sums.
- [Pluck values from list of dictionaries](https://www.wiki.jodisand.me/snippets/python/pluck/index.md): Converts a list of dictionaries into a list of values corresponding to the specified key.
- [Powerset](https://www.wiki.jodisand.me/snippets/python/powerset/index.md): Returns the powerset of a given iterable.
- [Radians to degrees](https://www.wiki.jodisand.me/snippets/python/rads_to_degrees/index.md): Converts an angle from radians to degrees.
- [Random element in list](https://www.wiki.jodisand.me/snippets/python/sample/index.md): Returns a random element from a list.
- [Remove list elements](https://www.wiki.jodisand.me/snippets/python/take/index.md): Returns a list with n elements removed from the beginning.
- [Remove list elements from the end](https://www.wiki.jodisand.me/snippets/python/take_right/index.md): Returns a list with n elements removed from the end.
- [Repeat string](https://www.wiki.jodisand.me/snippets/python/n_times_string/index.md): Generates a string with the given string value repeated n number of times.
- [Reverse compose functions](https://www.wiki.jodisand.me/snippets/python/compose_right/index.md): Performs left-to-right function composition.
- [Reverse list](https://www.wiki.jodisand.me/snippets/python/reverse/index.md): Reverses a list or a string.
- [Reverse number](https://www.wiki.jodisand.me/snippets/python/reverse_number/index.md): Reverses a number.
- [RGB to hex](https://www.wiki.jodisand.me/snippets/python/rgb_to_hex/index.md): Converts the values of RGB components to a hexadecimal color code.
- [Rotate list elements](https://www.wiki.jodisand.me/snippets/python/roll/index.md): Moves the specified amount of elements to the start of the list.
- [Shuffle list](https://www.wiki.jodisand.me/snippets/python/shuffle/index.md): Randomizes the order of the values of an list, returning a new list.
- [Snakecase string](https://www.wiki.jodisand.me/snippets/python/snake/index.md): Converts a string to snake case.
- [Sort dictionary by key](https://www.wiki.jodisand.me/snippets/python/sort_dict_by_key/index.md): Sorts the given dictionary by key.
- [Sort dictionary by value](https://www.wiki.jodisand.me/snippets/python/sort_dict_by_value/index.md): Sorts the given dictionary by value.
- [Sort list by indexes](https://www.wiki.jodisand.me/snippets/python/sort_by_indexes/index.md): Sorts one list based on another list containing the desired indexes.
- [Split into lines](https://www.wiki.jodisand.me/snippets/python/split_lines/index.md): Splits a multiline string into a list of lines.
- [Split list into chunks](https://www.wiki.jodisand.me/snippets/python/chunk/index.md): Chunks a list into smaller lists of a specified size.
- [Split list into n chunks](https://www.wiki.jodisand.me/snippets/python/chunk_into_n/index.md): Chunks a list into n smaller lists.
- [Spread list](https://www.wiki.jodisand.me/snippets/python/spread/index.md): Flattens a list, by spreading its elements into a new list.
- [String is anagram](https://www.wiki.jodisand.me/snippets/python/is_anagram/index.md): Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).
- [String to slug](https://www.wiki.jodisand.me/snippets/python/slugify/index.md): Converts a string to a URL-friendly slug.
- [String to words](https://www.wiki.jodisand.me/snippets/python/words/index.md): Converts a given string into a list of words.
- [Sum list based on function](https://www.wiki.jodisand.me/snippets/python/sum_by/index.md): Calculates the sum of a list, after mapping each element to a value using the provided function.
- [Sum of powers](https://www.wiki.jodisand.me/snippets/python/sum_of_powers/index.md): Returns the sum of the powers of all the numbers from start to end (both inclusive).
- [Test if every list element is falsy](https://www.wiki.jodisand.me/snippets/python/none/index.md): Checks if the provided function returns True for at least one element in the list.
- [Test if every list element is truthy](https://www.wiki.jodisand.me/snippets/python/every/index.md): Checks if the provided function returns True for every element in the list.
- [Test if some list elements are truthy](https://www.wiki.jodisand.me/snippets/python/some/index.md): Checks if the provided function returns True for at least one element in the list.
- [Transpose matrix](https://www.wiki.jodisand.me/snippets/python/transpose/index.md): Transposes a two-dimensional list.
- [Unfold list](https://www.wiki.jodisand.me/snippets/python/unfold/index.md): Builds a list, using an iterator function and an initial seed value.
- [Unique elements in list](https://www.wiki.jodisand.me/snippets/python/unique_elements/index.md): Returns the unique elements in a given list.
- [Value frequencies](https://www.wiki.jodisand.me/snippets/python/frequencies/index.md): Creates a dictionary with the unique values of a list as keys and their frequencies as the values.
- [Weighted average](https://www.wiki.jodisand.me/snippets/python/weighted_average/index.md): Returns the weighted average of two or more numbers.
