https://docs.python.org/3/library/functions.html
Builtin functions are always available.
# Builtin objects in Python 2. import __builtin__ # Py2 dir(__builtin__) # list of objects
# Builtin objects in Python 3. import builtins # Py3 dir(builtins) # list of objects zip is builtins.zip # True zip = my_better_zip
Type names: basestring(), bool(), bytearray(), bytes(), complex(), dict(), float(), frozenset(), int(), list(), long() [Py2], set(), str(), tuple(), unicode() [Py2].
Mathematical functions and tools: abs(), all(), any(), ascii() [Py3], bin() , chr(), divmod(), enumerate(), eval(), exec(), filter() format(), hex(), iter(), len(), map(), max(), min(), next() [Py2.6+], oct(), open(), ord(), pow(), range(), repr(), reversed(), round(), sorted(), sum(), unichr() [Py2], xrange() [Py2], zip().
OOP: classmethod(), delattr(), dir(), getattr(), hasattr(), isinstance(), issubclass(), object(), property(), setattr(), staticmethod(), super(), type().
# zip(*iterables) return a zip object # zip(*iterables, strict=False) in Py3.10 # The strict=True is used to test if the lengths of iterables are identical. # Iterate over several iterables in parallel, producing tuples with an item from each one. # By default, zip() stops when the shortest iterable is exhausted. list(zip([1, 2, 3], 'abc')) # [(1, 'a'), (2, 'b'), (3, 'c')] list(zip([1, 2], 'abcd')) # [(1, 'a'), (2, 'b')], different lengths
# map(function, iterable, ...) return a map object (iterator) in Py3 def cube(x): return x * x * x list(map(cube, range(10))) # map object list(cube(x) for x in range(10)) # generator object # [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
L = [(1, 2), (3, 4, 5)] list(map(sum, L)) # [3, 12] map object list(sum(t) for t in L) # [3, 12] generator object
L = [1, 2, 3] M = [4, 5, 6] list(map(sum, zip(L, M))) # [5, 7, 9] list(sum(t) for t in zip(L, M))
def calc(x, y): # two arguments and two iterables in map() return 2 * x + y R = range(10) list(map(calc, R, R)) list(calc(x, y) for (x, y) in zip(R, R)) # [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
# filter(function or None, iterable) return a filter object (iterator) in Py3 def check(x): return x % 2 != 0 and x % 3 != 0 list(filter(check, range(30))) # filter object list(x for x in range(30) if check(x)) # generator object, equivalent # [1, 5, 7, 11, 13, 17, 19, 23, 25, 29] list(filter(None, [-2,-1,0,1,2])) # [-2, -1, 1, 2] filter object list(x for x in [-2,-1,0,1,2] if x) # generator object, equivalent
# Testing speed. import random import timeit L = [random.random() for x in range(1000000)] func = (lambda x: x < 0.5) t1 = timeit.Timer(lambda: sum(x for x in L if x < 0.5)) print(t1.timeit(1)) # 0.028795670999897993 best t2 = timeit.Timer(lambda: sum(x for x in L if func(x))) print(t2.timeit(1)) # 0.06057641600000352 t3 = timeit.Timer(lambda: sum(filter((lambda x: x < 0.5), L))) print(t3.timeit(1)) # 0.049515714000108346 t4 = timeit.Timer(lambda: sum(filter(func, L))) print(t4.timeit(1)) # 0.0439489059999687