[Edu-sig] Itertools

Dethe Elza dethe.elza at blastradius.com
Tue Jan 27 12:56:05 EST 2004


> Thanks for showing the islice trick.  Have you made a study of 
> itertools?
> I'd be interested in learning other such tips, if you've got the time.

My pleasure. I'm really looking forward to Python 1.4 generator 
expressions, which will make the itertools module even more 
interesting.  Calling generators is more efficient (down in the guts of 
Python) than calling functions, and because they are lazily evaluated, 
they tend to be more efficient in both memory and time than lists are, 
while maintaining much of the same ease of use.

All of this is taken from the python docs, which covers all of the 
following functions and provides some useful examples.

Like the fibonacci generator, many of these functions return an 
unbounded (infinite) number of results, and should only be used in 
functions, loops, or other iterators which truncate the stream somehow 
(like islice in the example).

chain(iterable, [iterable,] ...)

Chain takes a list of iterables and returns one which will iterate over 
the first iterable until it is exhausted, then the second until it is 
exhausted, and so on.

count([n])

count will return consecutive integers until n is reached

cycle(iterable)

cycle will run through an iterable and save the values.  When the 
iterable is exhausted it repeats using the saved values, turning a 
finite iterable into an infinite one.

dropwhile(predicate, iterable)

dropwhile is a latch function.  It discards the results of the iterable 
as long as predictate(iterable.next()) returns true.  Once the 
predictate becomes false it will return the rest of the results.

ifilter(predicate, iterable)

only returns iterable results for which the predicate is true.  
Equivalent to the builtin filter, but for iterables (and thus lazily 
evaluated)

ifilterfalse(predicate, iterable)

like ifilter but only returns iterator results for which the predicate 
is false

imap(function, iterable [, iterable] ...)

imap is like the builtin map function, but takes a list of iterables 
rather than a list of sequences, and applys the function to the results 
of the iterable(s)

islice(iterable, [start], stop, [step])

islice takes a slice of an iterable, with optional start and step 
arguments.  Because iterables can be infinite in length it does not 
permit negative numbers for stop, start, or step arguments.

izip(iterable, [iterable], ...)

izip is like the builtin zip function, but takes a list of iterables 
rather than sequences

repeat(object, [times])

repeat returns an iterable which will return the same object over and 
over

starmap(function, iterable)

starmap is to imap as function(*args) is to function(a,b,c).  It 
expects a tuple from the iterable, which is passed in as the args of 
function.

takewhile(predicate, iterable)

takewhile returns an iterable of the results of its iterable, truncated 
at the first result which evaluates false in the predicate function.

All of that sounds very long-winded and obtuse perhaps.  The basic 
concept is that rather than having a list, where all the elements of 
the list are known and can be manipulated, you have a stream and the 
elements can be manipulated as you receive them from the stream.

I hope that's helpful.  I'm willing to clarify any confusions I've 
created with it.

--Dethe

"Debugging is twice as hard as writing the code in the first place. 
Therefore, if you write the code as cleverly as possible, you are, by 
definition, not smart enough to debug it. "  --Brian Kernighan




More information about the Edu-sig mailing list