istep() addition to itertool? (Was: Re: Printing n elements per line in a list)

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Aug 21 02:23:18 EDT 2006


In <1156137473.501267.43910 at p79g2000cwp.googlegroups.com>, Justin  Azoff
wrote:

> Rhamphoryncus wrote:
> [snip interesting istep function]
> 
>> Would anybody else find this useful?  Maybe worth adding it to itertool?
> 
> yeah, but why on earth did you make it so complicated?
> 
> def istep(iterable, step):
>     a=[]
>     for x in iterable:
>         if len(a) >= step:
>             yield a
>             a=[]
>         a.append(x)
>     if a:
>         yield a

This is not as "lazy" as Rhamphoryncus' function anymore.  Lets say the
`iterable` is a stream of events, then your function always needs to
"receive" `step` events before the caller can do anything else with the
events.  In Rhamphoryncus' function the caller can react on the event as
soon as it's "delivered" by `iterable`.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list