looping through a file

Steven Taschuk staschuk at telusplanet.net
Wed Apr 30 16:23:25 CEST 2003

Quoth Andrew Bennetts:
> I thought there might be something in 2.3's itertools module to help with
> this -- I was hoping you'd be able to write something like:
>     InFile = open('users.dat', 'r')
>     for username, password in multistep(InFile, 2):

Such a function (with a name something like group) has been
proposed, but I don't think it's in itertools yet.

> I couldn't see anything in itertools to save me writing that out.  But I
> just figured out how to do it with itertools -- izip can do it:
>     def multistep(iterable, count):
>         return itertools.izip(*((iter(iterable),)*count))

Heh.  That's a clever implementation.  (Perhaps too clever.)

> So it's probably not worth defining a seperate function...

Imho a separate function is desirable for clarity.

Steven Taschuk                           staschuk at telusplanet.net
"I'm always serious, never more so than when I'm being flippant."
                            -- _Look to Windward_, Iain M. Banks

More information about the Python-list mailing list