Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1 | \section{\module{itertools} --- |
| 2 | Functions creating iterators for efficient looping} |
| 3 | |
| 4 | \declaremodule{standard}{itertools} |
| 5 | \modulesynopsis{Functions creating iterators for efficient looping.} |
| 6 | \moduleauthor{Raymond Hettinger}{python@rcn.com} |
| 7 | \sectionauthor{Raymond Hettinger}{python@rcn.com} |
| 8 | \versionadded{2.3} |
| 9 | |
| 10 | |
| 11 | This module implements a number of iterator building blocks inspired |
| 12 | by constructs from the Haskell and SML programming languages. Each |
| 13 | has been recast in a form suitable for Python. |
| 14 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 15 | The module standardizes a core set of fast, memory efficient tools |
| 16 | that are useful by themselves or in combination. Standardization helps |
| 17 | avoid the readability and reliability problems which arise when many |
| 18 | different individuals create their own slightly varying implementations, |
| 19 | each with their own quirks and naming conventions. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 20 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 21 | The tools are designed to combine readily with each another. This makes |
| 22 | it easy to construct more specialized tools succinctly and efficiently |
| 23 | in pure Python. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 24 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 25 | For instance, SML provides a tabulation tool: \code{tabulate(\var{f})} |
| 26 | which produces a sequence \code{f(0), f(1), ...}. This toolbox |
| 27 | provides \function{imap()} and \function{count()} which can be combined |
| 28 | to form \code{imap(\var{f}, count())} and produce an equivalent result. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 29 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 30 | Whether cast in pure python form or C code, tools that use iterators |
| 31 | are more memory efficient (and faster) than their list based counterparts. |
| 32 | Adopting the principles of just-in-time manufacturing, they create |
| 33 | data when and where needed instead of consuming memory with the |
| 34 | computer equivalent of ``inventory''. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 35 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 36 | Some tools were omitted from the module because they offered no |
| 37 | advantage over their pure python counterparts or because their behavior |
| 38 | was too surprising. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 39 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 40 | For instance, SML provides a tool: \code{cycle(\var{seq})} which |
| 41 | loops over the sequence elements and then starts again when the |
| 42 | sequence is exhausted. The surprising behavior is the need for |
| 43 | significant auxiliary storage (which is unusual for an iterator). |
| 44 | If needed, the tool is readily constructible using pure Python. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 45 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 46 | Other tools are being considered for inclusion in future versions of the |
| 47 | module. For instance, the function |
| 48 | \function{chain(\var{it0}, \var{it1}, ...})} would return elements from |
| 49 | the first iterator until it was exhausted and then move on to each |
| 50 | successive iterator. The module author welcomes suggestions for other |
| 51 | basic building blocks. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 52 | |
| 53 | \begin{seealso} |
| 54 | \seetext{The Standard ML Basis Library, |
| 55 | \citetitle[http://www.standardml.org/Basis/] |
| 56 | {The Standard ML Basis Library}.} |
| 57 | |
| 58 | \seetext{Haskell, A Purely Functional Language, |
| 59 | \citetitle[http://www.haskell.org/definition/] |
| 60 | {Definition of Haskell and the Standard Libraries}.} |
| 61 | \end{seealso} |
| 62 | |
| 63 | |
| 64 | \subsection{Itertool functions \label{itertools-functions}} |
| 65 | |
| 66 | The following module functions all construct and return iterators. |
| 67 | Some provide streams of infinite length, so they should only be accessed |
| 68 | by functions or loops that truncate the stream. |
| 69 | |
| 70 | \begin{funcdesc}{count}{\optional{n}} |
| 71 | Make an iterator that returns consecutive integers starting with \var{n}. |
| 72 | Does not currently support python long integers. Often used as an |
| 73 | argument to \function{imap()} to generate consecutive data points. |
| 74 | Also, used in \function{izip()} to add sequence numbers. Equivalent to: |
| 75 | |
| 76 | \begin{verbatim} |
| 77 | def count(n=0): |
| 78 | cnt = n |
| 79 | while True: |
| 80 | yield cnt |
| 81 | cnt += 1 |
| 82 | \end{verbatim} |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 83 | |
| 84 | Note, \function{count()} does not check for overflow and will return |
| 85 | negative numbers after exceeding \code{sys.maxint}. This behavior |
| 86 | may change in the future. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 87 | \end{funcdesc} |
| 88 | |
| 89 | \begin{funcdesc}{dropwhile}{predicate, iterable} |
| 90 | Make an iterator that drops elements from the iterable as long as |
| 91 | the predicate is true; afterwards, returns every element. Note, |
| 92 | the iterator does not produce \emph{any} output until the predicate |
| 93 | is true, so it may have a lengthy start-up time. Equivalent to: |
| 94 | |
| 95 | \begin{verbatim} |
| 96 | def dropwhile(predicate, iterable): |
| 97 | iterable = iter(iterable) |
| 98 | while True: |
| 99 | x = iterable.next() |
| 100 | if predicate(x): continue # drop when predicate is true |
| 101 | yield x |
| 102 | break |
| 103 | while True: |
| 104 | yield iterable.next() |
| 105 | \end{verbatim} |
| 106 | \end{funcdesc} |
| 107 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 108 | \begin{funcdesc}{ifilter}{predicate, iterable} |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 109 | Make an iterator that filters elements from iterable returning only |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 110 | those for which the predicate is \code{True}. |
| 111 | If \var{predicate} is \code{None}, return the items that are true. |
| 112 | Equivalent to: |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 113 | |
| 114 | \begin{verbatim} |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 115 | def ifilter(predicate, iterable): |
| 116 | if predicate is None: |
| 117 | def predicate(x): |
| 118 | return x |
| 119 | for x in iterable: |
| 120 | if predicate(x): |
| 121 | yield x |
| 122 | \end{verbatim} |
| 123 | \end{funcdesc} |
| 124 | |
| 125 | \begin{funcdesc}{ifilterfalse}{predicate, iterable} |
| 126 | Make an iterator that filters elements from iterable returning only |
| 127 | those for which the predicate is \code{False}. |
| 128 | If \var{predicate} is \code{None}, return the items that are false. |
| 129 | Equivalent to: |
| 130 | |
| 131 | \begin{verbatim} |
| 132 | def ifilterfalse(predicate, iterable): |
| 133 | if predicate is None: |
| 134 | def predicate(x): |
| 135 | return x |
| 136 | for x in iterable: |
| 137 | if not predicate(x): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 138 | yield x |
| 139 | \end{verbatim} |
| 140 | \end{funcdesc} |
| 141 | |
| 142 | \begin{funcdesc}{imap}{function, *iterables} |
| 143 | Make an iterator that computes the function using arguments from |
| 144 | each of the iterables. If \var{function} is set to \code{None}, then |
| 145 | \function{imap()} returns the arguments as a tuple. Like |
| 146 | \function{map()} but stops when the shortest iterable is exhausted |
| 147 | instead of filling in \code{None} for shorter iterables. The reason |
| 148 | for the difference is that infinite iterator arguments are typically |
| 149 | an error for \function{map()} (because the output is fully evaluated) |
| 150 | but represent a common and useful way of supplying arguments to |
| 151 | \function{imap()}. |
| 152 | Equivalent to: |
| 153 | |
| 154 | \begin{verbatim} |
| 155 | def imap(function, *iterables): |
| 156 | iterables = map(iter, iterables) |
| 157 | while True: |
| 158 | args = [i.next() for i in iterables] |
| 159 | if function is None: |
| 160 | yield tuple(args) |
| 161 | else: |
| 162 | yield function(*args) |
| 163 | \end{verbatim} |
| 164 | \end{funcdesc} |
| 165 | |
| 166 | \begin{funcdesc}{islice}{iterable, \optional{start,} stop \optional{, step}} |
| 167 | Make an iterator that returns selected elements from the iterable. |
| 168 | If \var{start} is non-zero, then elements from the iterable are skipped |
| 169 | until start is reached. Afterward, elements are returned consecutively |
| 170 | unless \var{step} is set higher than one which results in items being |
| 171 | skipped. If \var{stop} is specified, then iteration stops at the |
| 172 | specified element position; otherwise, it continues indefinitely or |
| 173 | until the iterable is exhausted. Unlike regular slicing, |
| 174 | \function{islice()} does not support negative values for \var{start}, |
| 175 | \var{stop}, or \var{step}. Can be used to extract related fields |
| 176 | from data where the internal structure has been flattened (for |
| 177 | example, a multi-line report may list a name field on every |
| 178 | third line). Equivalent to: |
| 179 | |
| 180 | \begin{verbatim} |
| 181 | def islice(iterable, *args): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 182 | s = slice(*args) |
| 183 | next = s.start or 0 |
| 184 | stop = s.stop |
| 185 | step = s.step or 1 |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 186 | for cnt, element in enumerate(iterable): |
| 187 | if cnt < next: |
| 188 | continue |
| 189 | if cnt >= stop: |
| 190 | break |
| 191 | yield element |
| 192 | next += step |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 193 | \end{verbatim} |
| 194 | \end{funcdesc} |
| 195 | |
| 196 | \begin{funcdesc}{izip}{*iterables} |
| 197 | Make an iterator that aggregates elements from each of the iterables. |
| 198 | Like \function{zip()} except that it returns an iterator instead of |
| 199 | a list. Used for lock-step iteration over several iterables at a |
| 200 | time. Equivalent to: |
| 201 | |
| 202 | \begin{verbatim} |
| 203 | def izip(*iterables): |
| 204 | iterables = map(iter, iterables) |
| 205 | while True: |
| 206 | result = [i.next() for i in iterables] |
| 207 | yield tuple(result) |
| 208 | \end{verbatim} |
| 209 | \end{funcdesc} |
| 210 | |
| 211 | \begin{funcdesc}{repeat}{obj} |
| 212 | Make an iterator that returns \var{obj} over and over again. |
| 213 | Used as argument to \function{imap()} for invariant parameters |
| 214 | to the called function. Also used with function{izip()} to create |
| 215 | an invariant part of a tuple record. Equivalent to: |
| 216 | |
| 217 | \begin{verbatim} |
| 218 | def repeat(x): |
| 219 | while True: |
| 220 | yield x |
| 221 | \end{verbatim} |
| 222 | \end{funcdesc} |
| 223 | |
| 224 | \begin{funcdesc}{starmap}{function, iterable} |
| 225 | Make an iterator that computes the function using arguments tuples |
| 226 | obtained from the iterable. Used instead of \function{imap()} when |
| 227 | argument parameters are already grouped in tuples from a single iterable |
| 228 | (the data has been ``pre-zipped''). The difference between |
| 229 | \function{imap()} and \function{starmap} parallels the distinction |
| 230 | between \code{function(a,b)} and \code{function(*c)}. |
| 231 | Equivalent to: |
| 232 | |
| 233 | \begin{verbatim} |
| 234 | def starmap(function, iterable): |
| 235 | iterable = iter(iterable) |
| 236 | while True: |
| 237 | yield function(*iterable.next()) |
| 238 | \end{verbatim} |
| 239 | \end{funcdesc} |
| 240 | |
| 241 | \begin{funcdesc}{takewhile}{predicate, iterable} |
| 242 | Make an iterator that returns elements from the iterable as long as |
| 243 | the predicate is true. Equivalent to: |
| 244 | |
| 245 | \begin{verbatim} |
| 246 | def takewhile(predicate, iterable): |
| 247 | iterable = iter(iterable) |
| 248 | while True: |
| 249 | x = iterable.next() |
| 250 | if predicate(x): |
| 251 | yield x |
| 252 | else: |
| 253 | break |
| 254 | \end{verbatim} |
| 255 | \end{funcdesc} |
| 256 | |
| 257 | \begin{funcdesc}{times}{n, \optional{object}} |
| 258 | Make an iterator that returns \var{object} \var{n} times. |
| 259 | \var{object} defaults to \code{None}. Used for looping a specific |
| 260 | number of times without creating a number object on each pass. |
| 261 | Equivalent to: |
| 262 | |
| 263 | \begin{verbatim} |
| 264 | def times(n, object=None): |
| 265 | if n<0 : raise ValueError |
| 266 | for i in xrange(n): |
| 267 | yield object |
| 268 | \end{verbatim} |
| 269 | \end{funcdesc} |
| 270 | |
| 271 | |
| 272 | \subsection{Examples \label{itertools-example}} |
| 273 | |
| 274 | The following examples show common uses for each tool and |
| 275 | demonstrate ways they can be combined. |
| 276 | |
| 277 | \begin{verbatim} |
| 278 | >>> for i in times(3): |
| 279 | ... print "Hello" |
| 280 | ... |
| 281 | Hello |
| 282 | Hello |
| 283 | Hello |
| 284 | |
| 285 | >>> amounts = [120.15, 764.05, 823.14] |
| 286 | >>> for checknum, amount in izip(count(1200), amounts): |
| 287 | ... print 'Check %d is for $%.2f' % (checknum, amount) |
| 288 | ... |
| 289 | Check 1200 is for $120.15 |
| 290 | Check 1201 is for $764.05 |
| 291 | Check 1202 is for $823.14 |
| 292 | |
| 293 | >>> import operator |
| 294 | >>> for cube in imap(operator.pow, xrange(1,4), repeat(3)): |
| 295 | ... print cube |
| 296 | ... |
| 297 | 1 |
| 298 | 8 |
| 299 | 27 |
| 300 | |
| 301 | >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', |
| 302 | '', 'martin', '', 'walter', '', 'samuele'] |
| 303 | >>> for name in islice(reportlines, 3, len(reportlines), 2): |
| 304 | ... print name.title() |
| 305 | ... |
| 306 | Alex |
| 307 | Laura |
| 308 | Martin |
| 309 | Walter |
| 310 | Samuele |
| 311 | |
| 312 | \end{verbatim} |
| 313 | |
| 314 | This section has further examples of how itertools can be combined. |
| 315 | Note that \function{enumerate()} and \method{iteritems()} already |
| 316 | have highly efficient implementations in Python. They are only |
| 317 | included here to illustrate how higher level tools can be created |
| 318 | from building blocks. |
| 319 | |
| 320 | \begin{verbatim} |
| 321 | >>> def enumerate(iterable): |
| 322 | ... return izip(count(), iterable) |
| 323 | |
| 324 | >>> def tabulate(function): |
| 325 | ... "Return function(0), function(1), ..." |
| 326 | ... return imap(function, count()) |
| 327 | |
| 328 | >>> def iteritems(mapping): |
| 329 | ... return izip(mapping.iterkeys(), mapping.itervalues()) |
| 330 | |
| 331 | >>> def nth(iterable, n): |
| 332 | ... "Returns the nth item" |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 333 | ... return list(islice(iterable, n, n+1)) |
| 334 | |
| 335 | >>> def all(pred, seq): |
| 336 | ... "Returns True if pred(x) is True for every element in the iterable" |
| 337 | ... return not nth(ifilterfalse(pred, seq), 0) |
| 338 | |
| 339 | >>> def some(pred, seq): |
| 340 | ... "Returns True if pred(x) is True at least one element in the iterable" |
| 341 | ... return bool(nth(ifilter(pred, seq), 0)) |
| 342 | |
| 343 | >>> def no(pred, seq): |
| 344 | ... "Returns True if pred(x) is False for every element in the iterable" |
| 345 | ... return not nth(ifilter(pred, seq), 0) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 346 | |
| 347 | \end{verbatim} |