Python Extended

Common Python methods organized by module.

Method Module Description
functools.partial(function, *args, **keywords) functools Returns a new partial object which when called will behave like func called with the positional arguments args and keyword arguments keywords.
functools.wraps(function) functools Decorator used to update the metadata (such as __name__, __doc__) of the wrapper function to look more like the wrapped function.
functools.reduce(function, iterable) functools Applies function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value.
functools.lru_cache(maxsize=None, typed=False) functools Decorator to wrap a function with a memoizing callable that saves up to the maxsize most recent calls. It can save time when an expensive or I/O bound function is periodically called with the same arguments.
zip(*iterables, strict=False) iterables Returns an iterator that aggregates elements from each of the iterables. It continues until the shortest iterable is exhausted, and if strict is True, raises a ValueError if the iterables are of unequal length.
any(iterable) iterables Returns True if at least one element in the iterable is True. Otherwise, returns False.
all(iterable) iterables Returns True if all elements in the iterable are True, or if the iterable is empty. Otherwise, returns False.
enumerate(iterable, start=0) iterables Returns an iterator that yields tuples containing an index and the value of each item in the iterable. The index starts at the specified start value (default is 0).
map(function, iterable, *iterables) iterables Returns an iterator that applies the specified function to each item in the iterable(s) and yields the result. If multiple iterables are passed, the function is called with corresponding items from each iterable.
itertools.product(*iterables, repeat=1) itertools Returns the Cartesian product of the input iterables, yielding tuples containing elements from each iterable.
itertools.permutations(iterable, r=None) itertools Returns all possible r-length tuples of elements from the iterable.
itertools.combinations(iterable, r) itertools Returns all possible r-length combinations of elements from the iterable.
itertools.combinations_with_replacement(iterable, r) itertools Returns all possible r-length combinations of elements from the iterable, allowing individual elements to be repeated.
itertools.accumulate(iterable, func=operator.add) itertools Returns an iterator that produces accumulated sums (or other binary functions) of the elements.
itertools.groupby(iterable, key=None) itertools Returns an iterator that generates tuples of a key and an iterator of grouped items from the input iterable. The key function defaults to None for a simple grouping based on the identity of the items.
collections.Counter collections A dict subclass for counting hashable objects. It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values.
collections.OrderedDict collections A dict subclass that maintains the order of insertion of its keys.
collections.ChainMap collections A class for quickly combining several mappings into one single mapping.
collections.defaultdict collections A subclass of the built-in dict class that returns a default value when a non-existent key is accessed.
collections.namedtuple collections A factory function for creating tuple subclasses with named fields, which increases code readability and maintainability.
random.random() random Returns a random floating point number in the range (0.0, 1.0).
random.randint(a, b) random Returns a random integer between a and b (inclusive).
random.choice(seq) random Returns a random element from the non-empty sequence.
random.shuffle(seq) random Randomly shuffles the elements of the sequence in place.
random.sample(seq, k) random Returns a list of k unique elements chosen randomly from the population sequence or set.
random.randrange(start, stop, step) random Returns a randomly selected element from the range(start, stop, step).
abs(x) numeric Returns the absolute value of x.
min(iterable) numeric Returns the smallest item in the iterable.
max(iterable) numeric Returns the largest item in the iterable.
pow(x, y) numeric Returns x raised to the power of y.
divmod(x, y) numeric Returns the quotient and remainder of the division of x by y.
math.ceil(x) numeric Returns the smallest integer greater than or equal to x.
math.floor(x) numeric Returns the largest integer less than or equal to x.
math.sqrt(x) numeric Returns the square root of x.
math.sin(x) numeric Returns the sine of x (in radians).
math.cos(x) numeric Returns the cosine of x (in radians).
math.tan(x) numeric Returns the tangent of x (in radians).
statistics.mean(iterable) numeric Returns the arithmetic mean of the iterable.
statistics.median(iterable) numeric Returns the median (middle value) of the iterable.