Hi there! At one point, while using Python, have you felt the need of a syntactically short way to quickly perform an operation on a list, and you’ve wondered, with Python’s excellent built-in functions(fine, “methods” – OK, Java developers ;)), if functions exist to do things like, get all of the permutations or combinations of a list with or without replacement; evaluate a common function(a higher-order function) to every element in a list; get a tuple of all possible outfits, given lists of shirts, trousers and ties?
If you have, then itertools is the module for you! With functions that perform operations taught frequently in high-school math classes, like Cartesian Products, Permutations, Combinations and Set-Building, itertools enables you to succinctly produce a list from another list, without getting your head flipped with nested listcomps and complex generator expressions. I’m going to go through a few useful functions here. Check out the Python Docs for more info:
Some of you may recall map(), that in Python 3 has been relegated to functools from the global namespace. This function applies a given function(including lambda/anonymous functions) to a set. The difference between imap and map is that imap is evaluated lazily and returns an iterator. You need to keep calling next on the iterator to get each value, whereas, map evaluates the function on all elements of the specified list immediately. Thus, imap is suitable for situations in which you have large lists but don’t need the values all at once.
This returns the cartesian product of lists passed to it. This is quite handy while filling out a form automatically through a script.
>>> import itertools >>> shirts = ['green shirt','red shirt'] >>> trousers = ['corduroy trousers', 'pinstriped trousers'] >>> ties = ['bow tie', 'cravate'] >>> outfits = itertools.product(shirts, trousers, ties) >>> for e in outfits: print e('green shirt', 'corduroy trousers', 'bow tie') ('green shirt', 'corduroy trousers', 'cravate') ('green shirt', 'pinstriped trousers', 'bow tie') ('green shirt', 'pinstriped trousers', 'cravate') ('red shirt', 'corduroy trousers', 'bow tie') ('red shirt', 'corduroy trousers', 'cravate') ('red shirt', 'pinstriped trousers', 'bow tie') ('red shirt', 'pinstriped trousers', 'cravate')
combinatoric(iterable, r) where combinatoric stands for any of the following – combinations, combinations_with_replacement or permutations,
and r is the length of each subsequence. For example,
>>> comb= itertools.combinations([1,2,3], 2) >>> [x for x in comb] [(1, 2), (1, 3), (2, 3)]
ifilter(predicate, iterable) and
ifilterfalse(predicate, iterable)These two functions generate iterators that filter based on the expression ‘predicate’. ‘ifilter’ returns elements for which the predicate/condition is true and ‘ifilterfalse’ returns elements for which the predicate/condition evaluates to false.
I hope this helped you increase your program efficiency and added to your list of tools that you can use while tackling a problem. Don’t forget to check out the Python Docs for the other nifty functions.
And now for something completely different…
For those of you may point point out the elegance of list comprehensions, wait up! New posts are coming soon.