Kahless Kahless - 3 months ago 7
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]:
tottalWordlist.append(gWord)
break

Answer

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.