A list of Python functions that make life easier.
Photo by Artturi Jalli on Unsplash
1. enumerate(iterable, start=0)
This function creates a enumerate object which takes in an iterable sequence.
We can use this function often when we need the iterator along with the value unlike
>>> range(len(list))
Example:
>>> chessPieces = ["Pawns", "Rook", "Horse", "Bishops", "King", "Queen"]
>>> list(enumerate(chessPieces))
[(0, 'Pawns'), (1, 'Rook'), (2, 'Horse'), (3, 'Bishops'), (4, 'King'), (5, 'Queen')]
>>> list(enumerate(chessPieces, start = 1))
[(1, 'Pawns'), (2, 'Rook'), (3, 'Horse'), (4, 'Bishops'), (5, 'King'), (6, 'Queen')]
2. Zip(*iterable, strict=False)
This function is used to zip together two or more iterable sequences.
We can use this function when combining data from two or more sources like two CSV files, or two or more lists, etc.
Example:
>>> listOfUsers = ['a', 'b', 'c']
>>> listOfResponse = ['I m a', 'I m b', 'I m c']
>>> for user, response in zip(listOfUsers, listOfResponse):
>>> print(user, ': ', response)
a : I m a
b : I m b
c : I m c
So any edge cases when you use this function?
Yes, there are a few things to look out for when using this function.
When combining two or more iterable by default the function stops at the smallest length.
>>> listOfUsers = ['a', 'b', 'c', 'd']
>>> listOfResponse = ['I m a', 'I m b', 'I m c']
>>> for user, response in zip(listOfUsers, listOfResponse):
>>> print(user, ': ', response)
a : I m a
b : I m b
c : I m c
This will cause bugs in your program if forget this, so you can pass in —
Strict = True
So that if you the pass values, Python will raise
ValueError: zip() argument 1 is longer than argument 2
3. __import__(name, globals=None, locals=None, fromlist=(), level=0)
This function is really good if you want to dynamically install a module using subprocess(“pip install module”) and then import this into your program.
level specifies whether to use absolute or relative imports. 0
(the default) means only perform absolute imports. Positive values for level indicate the number of parent directories to search relative to the directory of the module called.
Example:
- import test
testModule = __import__('test', globals(), locals(), [], 0)
2. import test.test2
testModule = __import__('test.test2', globals(), locals(), [], 0)
3. from test import func1
testModule = __import__('test', globals(), locals(), ['func1'], 0)
However, PEP 302 introduces Import Hooks and you can check out the examples there.
4. accumulate(iterable, func=operator.add, *, initial=None)
This function returns accumulated sums or accumulated results.
accumulate([1,2,3,4,5]) --> 1 3 6 10
accumulate([1,2,3,4,5], initial=100) --> 100 101 103 106 110 accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
5. collections.namedtuple(typename, field_names, *, rename\=False, defaults\=None, module\=None)
Named tuples are a self-documenting way of creating tuples, this allows us to access fields by name instead of position index.
The field_names are a sequence of strings such as [‘a’,’b’] alternately can also be written can be written as [‘a b’].
If rename = True then duplicate field names and keywords will be replaced automatically.
Eg: If the field names are ['x', 'y', 'z']
and the defaults are (1, 2)
, then x
will be a required argument, y
will default to 1
, and z
will default to 2
.
defaults = None or Iterable sequence, for eg [‘x y’] has defaults (1,2) then x = 1, y = 2.
To find more about these or inbuilt functions, check out:
Built-in Functions - Python 3.10.0 documentation
*The Python interpreter has several functions and types built into it that are always available. They are listed…*docs.python.org