[Tutor] Multiple Simultaneous Loops

Andre Engels andreengels at gmail.com
Thu Sep 15 11:26:39 CEST 2005


On 9/15/05, Ed Singleton <singletoned at gmail.com> wrote:
> I roughly want to be able to do:
> 
> for x, y 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?

It's not fully clear to me what you want to do. Do you want to go
through each pair x,y with x in bunch_of_files and y in range(z)? Then
you can do:

for x in bunch_of_files:
    for y in range(z):

Or do you want to have one value of y for each value of x? In that
case I think you'd want:
for y in range(len(bunch_of_files)):
    x = bunch_of_files[y]

> If so, what would be the best way to create a range of indeterminate length?

I don't think such a thing exists. Either it has some length or it
does not. There's nothing in between.

> 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).

See my second example above?

Andre Engels


More information about the Tutor mailing list