[Tutor] Multiple Simultaneous Loops
kent37 at tds.net
Thu Sep 15 11:44:09 CEST 2005
Ed Singleton wrote:
> I roughly want to be able to do:
> for f, x in bunch_of_files, range(z):
> so that x iterates through my files, and y iterates through something else.
> Is this something I can do?
In the general case use zip():
for f, x in zip(bunch_of_files, range(z)):
In this case, where the second item is just the index to the loop, use enumerate() instead of range() and zip()
for x, f in enumerate(bunch_of_files):
> If so, what would be the best way to create a range of indeterminate length?
itertools.count() generates an "unlimited" sequence.
> If not, is there a nice way I can do it, rather than than incrementing
> a variable (x = x + 1) every loop?
> Or maybe can I access the number of times the loop has run? ('x = x +
> 1' is so common there must be some more attractive shortcut).
> So far in learning Python I've founbd that when I feel you should be
> able to do something, then you can.
More information about the Tutor