| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="module-ConfigParser.html" /> |
| 13 | <link rel="prev" href="module-sets.html" /> |
| 14 | <link rel="parent" href="misc.html" /> |
| 15 | <link rel="next" href="itertools-functions.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>5.16 itertools -- Functions creating iterators for efficient looping</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="5.15.4 Comparison to the" |
| 25 | href="comparison-to-builtin-set.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services" |
| 28 | href="misc.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="5.16.1 Itertool functions" |
| 31 | href="itertools-functions.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="comparison-to-builtin-set.html">5.15.4 Comparison to the</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="itertools-functions.html">5.16.1 Itertool functions</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION0071600000000000000000"> |
| 56 | 5.16 <tt class="module">itertools</tt> -- |
| 57 | Functions creating iterators for efficient looping</A> |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <A NAME="module-itertools"></A> |
| 62 | |
| 63 | <span class="versionnote">New in version 2.3.</span> |
| 64 | |
| 65 | <P> |
| 66 | This module implements a number of iterator building blocks inspired |
| 67 | by constructs from the Haskell and SML programming languages. Each |
| 68 | has been recast in a form suitable for Python. |
| 69 | |
| 70 | <P> |
| 71 | The module standardizes a core set of fast, memory efficient tools |
| 72 | that are useful by themselves or in combination. Standardization helps |
| 73 | avoid the readability and reliability problems which arise when many |
| 74 | different individuals create their own slightly varying implementations, |
| 75 | each with their own quirks and naming conventions. |
| 76 | |
| 77 | <P> |
| 78 | The tools are designed to combine readily with one another. This makes |
| 79 | it easy to construct more specialized tools succinctly and efficiently |
| 80 | in pure Python. |
| 81 | |
| 82 | <P> |
| 83 | For instance, SML provides a tabulation tool: <code>tabulate(f)</code> |
| 84 | which produces a sequence <code>f(0), f(1), ...</code>. This toolbox |
| 85 | provides <tt class="function">imap()</tt> and <tt class="function">count()</tt> which can be combined |
| 86 | to form <code>imap(f, count())</code> and produce an equivalent result. |
| 87 | |
| 88 | <P> |
| 89 | Likewise, the functional tools are designed to work well with the |
| 90 | high-speed functions provided by the <tt class="module"><a href="module-operator.html">operator</a></tt> module. |
| 91 | |
| 92 | <P> |
| 93 | The module author welcomes suggestions for other basic building blocks |
| 94 | to be added to future versions of the module. |
| 95 | |
| 96 | <P> |
| 97 | Whether cast in pure python form or compiled code, tools that use iterators |
| 98 | are more memory efficient (and faster) than their list based counterparts. |
| 99 | Adopting the principles of just-in-time manufacturing, they create |
| 100 | data when and where needed instead of consuming memory with the |
| 101 | computer equivalent of ``inventory''. |
| 102 | |
| 103 | <P> |
| 104 | The performance advantage of iterators becomes more acute as the number |
| 105 | of elements increases - at some point, lists grow large enough to |
| 106 | severely impact memory cache performance and start running slowly. |
| 107 | |
| 108 | <P> |
| 109 | <div class="seealso"> |
| 110 | <p class="heading">See Also:</p> |
| 111 | |
| 112 | <div class="seetext"><p>The Standard ML Basis Library, |
| 113 | <em class="citetitle"><a |
| 114 | href="http://www.standardml.org/Basis/" |
| 115 | title="The Standard ML Basis Library" |
| 116 | >The Standard ML Basis Library</a></em>.</p></div> |
| 117 | |
| 118 | <P> |
| 119 | <div class="seetext"><p>Haskell, A Purely Functional Language, |
| 120 | <em class="citetitle"><a |
| 121 | href="http://www.haskell.org/definition/" |
| 122 | title="Definition of Haskell and the Standard Libraries" |
| 123 | >Definition of Haskell and the Standard Libraries</a></em>.</p></div> |
| 124 | </div> |
| 125 | |
| 126 | <P> |
| 127 | |
| 128 | <p><br /></p><hr class='online-navigation' /> |
| 129 | <div class='online-navigation'> |
| 130 | <!--Table of Child-Links--> |
| 131 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> |
| 132 | |
| 133 | <UL CLASS="ChildLinks"> |
| 134 | <LI><A href="itertools-functions.html">5.16.1 Itertool functions</a> |
| 135 | <LI><A href="itertools-example.html">5.16.2 Examples</a> |
| 136 | <LI><A href="itertools-recipes.html">5.16.3 Recipes</a> |
| 137 | </ul> |
| 138 | <!--End of Table of Child-Links--> |
| 139 | </div> |
| 140 | |
| 141 | <DIV CLASS="navigation"> |
| 142 | <div class='online-navigation'> |
| 143 | <p></p><hr /> |
| 144 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 145 | <tr> |
| 146 | <td class='online-navigation'><a rel="prev" title="5.15.4 Comparison to the" |
| 147 | href="comparison-to-builtin-set.html"><img src='../icons/previous.png' |
| 148 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 149 | <td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services" |
| 150 | href="misc.html"><img src='../icons/up.png' |
| 151 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 152 | <td class='online-navigation'><a rel="next" title="5.16.1 Itertool functions" |
| 153 | href="itertools-functions.html"><img src='../icons/next.png' |
| 154 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 155 | <td align="center" width="100%">Python Library Reference</td> |
| 156 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 157 | href="contents.html"><img src='../icons/contents.png' |
| 158 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 159 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 160 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 161 | <td class='online-navigation'><a rel="index" title="Index" |
| 162 | href="genindex.html"><img src='../icons/index.png' |
| 163 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 164 | </tr></table> |
| 165 | <div class='online-navigation'> |
| 166 | <b class="navlabel">Previous:</b> |
| 167 | <a class="sectref" rel="prev" href="comparison-to-builtin-set.html">5.15.4 Comparison to the</A> |
| 168 | <b class="navlabel">Up:</b> |
| 169 | <a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A> |
| 170 | <b class="navlabel">Next:</b> |
| 171 | <a class="sectref" rel="next" href="itertools-functions.html">5.16.1 Itertool functions</A> |
| 172 | </div> |
| 173 | </div> |
| 174 | <hr /> |
| 175 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 176 | </DIV> |
| 177 | <!--End of Navigation Panel--> |
| 178 | <ADDRESS> |
| 179 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 180 | </ADDRESS> |
| 181 | </BODY> |
| 182 | </HTML> |