Tutorial by Examples

For every infix operator, e.g. + there is a operator-function (operator.add for +): 1 + 1 # Output: 2 from operator import add add(1, 1) # Output: 2 even though the main documentation states that for the arithmetic operators only numerical input is allowed it is possible: from operator impo...
Instead of this lambda-function that calls the method explicitly: alist = ['wolf', 'sheep', 'duck'] list(filter(lambda x: x.startswith('d'), alist)) # Keep only elements that start with 'd' # Output: ['duck'] one could use a operator-function that does the same: from operator import metho...
Grouping the key-value pairs of a dictionary by the value with itemgetter: from itertools import groupby from operator import itemgetter adict = {'a': 1, 'b': 5, 'c': 1} dict((i, dict(v)) for i, v in groupby(adict.items(), itemgetter(1))) # Output: {1: {'a': 1, 'c': 1}, 5: {'b': 5}} which ...

Page 1 of 1