taking control of a for loop (python)
for-loop, python
Solution
I recommend putting the inner two loops into an appropriately-named function. When you find the key, return from the function.
list1=[var1,var2,var3]
list2 = [file1,file2,file3]
for v in list1:
//do stuff that returns a string, string.splitlines()
find_line(string)
and then:
def find_line(string):
for f in list2:
for line in string
if key in line and f in line:
print "line"
return
else:
continue
Problem
I have program which contains a for loop inside another for loop, which produces a line and i have an if statement that does a check for a key in the line. here is an example ``` list1=[var1,var2,var3] list2 = [file1,file2,file3] for v in list1: //do stuff that returns a string, string.splitlines() for f in list2: for line in string if key in line and f in line: print "line" break else: continue ``` I get the result I'm looking for, but I want to control the loop, by that I mean in the first iteration we have var1 and the it will loop through file1,file2 and file3 and then next iteration var2 and it will loop through file1,file2,file3 so on and so forth How will I change it so that when it finds the line, it won't go further with var1 instead takes var2 and then proceed ? I tried following ``` i = iter(list1) j = iter(list2) ``` and the following after print line ``` if key in line and f in line: print "line" i.next() j.next() break ``` But this doesn't seem to do anything, any tips on how to achieve this ?