Python’sitertools library is a gem - you can compose elegant solutionsfor a variety of problems with the functions it provides. Inmore-itertoolswe collect additional building blocks, recipes, and routines for working withPython iterables.
To get started, install the library withpip:
pipinstallmore-itertools
The recipes from theitertools docsare included in the top-level package:
>>>frommore_itertoolsimportflatten>>>iterable=[(0,1),(2,3)]>>>list(flatten(iterable))[0, 1, 2, 3]
Several new recipes are available as well:
>>>frommore_itertoolsimportchunked>>>iterable=[0,1,2,3,4,5,6,7,8]>>>list(chunked(iterable,3))[[0, 1, 2], [3, 4, 5], [6, 7, 8]]>>>frommore_itertoolsimportspy>>>iterable=(x*xforxinrange(1,6))>>>head,iterable=spy(iterable,n=3)>>>list(head)[1, 4, 9]>>>list(iterable)[1, 4, 9, 16, 25]
For the full listing of functions, see theAPI documentation.
Blog posts aboutmore-itertools:
more-itertools is maintained by@erikroseand@bbayles, with help frommany others.If you have a problem or suggestion, please file a bug or pull request in thisrepository. Thanks for contributing!
The version history can be found indocumentation.