Kahless Kahless - 1 year ago 78
Python Question

Start inner loop from position of the outer

In my example I have two loops. One nested within the other. Is there a way I can start the inner loop from the index of the outer. Here is pseudo code.

arrayOfWords = ["one","two","five"]
arrayOfWords2 = ["one","two","three","four","five"]
tottalWordlist = []
for index, jString in enumerate(arrayOfWords):
gWord = arrayOfWords[index]
indexClone = index
arrayOfWords2Count = range(len(arrayOfWords2)-1)
for indexClone in arrayOfWords2Count:
if gWord == arrayOfWords2[indexClone]:

Answer Source

You're almost there. You just need to add the starting index to the range object.

arrayOfWords2Count = range(indexClone, len(arrayOfWords2))
for i in arrayOfWords2Count:
    if gWord == arrayOfWords2[i]:

Also, you don't need to subtract one from the endpoint, as that end is exclusive.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download