Python removal of items after iteration
<p>Why is my code not working? I can't figure it out</p>

<pre><code>matchList = [['g', 'h', 'i'], ['a', 'b', 'c'] ... ]]
myList = []

for i in match_list:
    for j in i:
        for word in first_list:
            if j in word:
                myList.append(j)
                match_list.remove(j)
</code></pre>

<p>I am getting the error <code>ValueError: list.remove(x): x not in list</code></p>

<p>EDIT:</p>

<pre><code>first_list = ['i', 'am', 'an', 'old', 'man']
</code></pre>

<pre><code>list_of_lists = [['g', 'h', 'i'], ['a', 'b', 'c'] ... ]]
first_list = ['i', 'am', 'an', 'old', 'man']

myList = []

for current_list in list_of_lists:
    for item in current_list:
        for word in first_list:
            if item in word:
                myList.append(item)
                list_of_lists.remove(item)
</code></pre>

<p>I edited your code so it would read more sensibly, to me, so I can think I know what you're trying to do.</p>

<p>In my mind, you are reading through a list, from within a set of lists to check to see if an item from that "sublist" is in another list (first_list).</p>

<p>If the item is in one of the words in first list, you are trying to remove the "sublist" from list_of_lists, correct? If so, that's where you are getting your error. Say you are on the first iteration of the set of lists, and you are checking to see if the letter "i" from the first sublist in list_of_lists is in the first word from first_list. It should evaluate to true. At that point, you are then telling Python the following in plain english, "Python, remove the item from list_of_lists that has a value of i." However, because list_of_lists contains lists, that won't work, because none of the values within list_of_lists are strings, just lists.</p>

<p>What you need to do is specify the list you are currently iterating on, which will be represented by current_list in the above code.</p>

<p>So maybe instead of </p>

<pre><code>list_of_lists.remove(item)
</code></pre>

<p>or rather in your code</p>

<pre><code>match_list.remove(j) 
</code></pre>

<p>try </p>

<pre><code>list_of_lists.remove(current_list) 
</code></pre>

<p>or rather in your code </p>

<pre><code>match_list.remove(i).
</code></pre>

<p>I have a feeling you are going to experience other errors with the iterations that will likely remain in some scenarios if you get another match, but that's not the problem you're having now....</p>

python