comparing lists and storing index values if lists match
<p>I have <em>two</em> lists:</p>

<ul>
<li>wordsindict</li>
<li><p>list2</p>

<pre><code>wordsindict = ['somewhere', 'over', 'rainbow', 'bluebirds', 'fly', 'birds', 'fly', 'over', 'rainbow', 'why', 'why', 'double', 'time', 'population', 'long', 'takes', 'population', 'double', 'size', 'whoa', 'full', 'rainbow', 'way', 'double', 'rainbow', 'double', 'rainbow', 'way']

list2 = [['somewhere', 'over', 'rainbow', 'bluebirds', 'fly', 'birds', 'fly', 'over', 'rainbow', 'why', 'why'], ['double', 'time', 'population', 'long', 'takes', 'population', 'double', 'size'], ['whoa', 'full', 'rainbow', 'way', 'double', 'rainbow', 'double', 'rainbow', 'way']]
</code></pre></li>
</ul>

<p>I am taking the words(removing duplicates) that are within <strong>wordsindict</strong> and seeing if they are contained within list2. If they are, I wish to take the <strong>index value</strong> of the word in <strong>wordsindict</strong>. Beneath is the code that I currently have:</p>

<pre><code>listindex = {}
for word in wordsindict:
    listindex[word] = []
    for splittedLines_list in list2:
        index_list = []
        for i,j in enumerate(splittedLines_list):
            if j == word:
                index_list.append(i)
        listindex[word].append(index_list)
</code></pre>

<p>this code produces this output:</p>

<pre><code>{'fly': [[4, 6], [], []], 'rainbow': [[2, 8], [], [2, 5, 7]], 'full': [[], [], [1]], 'bluebirds': [[3], [], []], 'takes': [[], [4], []], 'somewhere': [[0], [], []], 'double': [[], [0, 6], [4, 6]], 'over': [[1, 7], [], []], 'long': [[], [3], []], 'why': [[9, 10], [], []], 'whoa': [[], [], [0]], 'way': [[], [], [3, 8]], 'time': [[], [1], []], 'size': [[], [7], []], 'birds': [[5], [], []], 'population': [[], [2, 5], []]}
</code></pre>

<p>it takes the words from wordsindict and stores their index value. This is incorrect as there are only 3 sublists within list2. It gives each index value its own list:</p>

<p><strong>e.g.</strong> <code>'population': [[], [2, 5], []</code> </p>

<pre><code>                     ^     ^     ^
                     0     1     2
</code></pre>

<p>Here you can see that population does appear within the first index value, but instead the words index value within the second sublist is recorded instead of simply <code>'population': [1, 1]</code>. </p>

<p>Put simply, I want the index value from list2 (0-2) to be appended, and if the word from wordsindict does appear more than once in list2 then append the index value again from where it was found.</p>

<p>wordsindict contains they keys and list2 should be searched for the occurrences.</p>

<p>If you need any more information, please do not hesitate to ask!</p>