0

Why is my code not working? I can't figure it out

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)

I am getting the error ValueError: list.remove(x): x not in list

EDIT:

first_list = ['i', 'am', 'an', 'old', 'man']
oneman
  • 811
  • 1
  • 13
  • 31
  • what is first_list? what data does it contain? – 0xhughes Oct 11 '16 at 03:06
  • j is the letter being iterated over from the list within the matchList, list. When you try to remove from match_list (presumably matchList above), there will be no j at that time. For instance, the letter "g" being assigned to object j, is not an item from the match_list that can be removed, because match_list is full of lists, not individual letters as seen in the lists residing in match_list. Does that make sense? – 0xhughes Oct 11 '16 at 03:08
  • Okay so it can't be deleted after its been iterated over like I'm trying to do? – oneman Oct 11 '16 at 03:10

4 Answers4

2

You want:

i.remove(j)

match_list is a list of lists

Him
  • 5,257
  • 3
  • 26
  • 83
0
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)

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

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).

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.

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.

So maybe instead of

list_of_lists.remove(item)

or rather in your code

match_list.remove(j) 

try

list_of_lists.remove(current_list) 

or rather in your code

match_list.remove(i).

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....

0xhughes
  • 2,703
  • 4
  • 26
  • 38
  • I still get the same error. I have tried your method, and like yourself I did not see any fault in the code yet it still gives me the same error, I am lost for ideas on what is going wrong – oneman Oct 11 '16 at 03:24
0

Try this:

btw: you used matchList and match_list in your post. I corrected that for my answer to make it consistent.

matchList = [['g', 'h', 'i'], ['a', 'b', 'c']]
myList = []
first_list = ['i', 'am', 'an', 'old', 'man']
for i in matchList:
    for j in i:
        for word in first_list:
            if j in word:
                myList.append(j)
                for s in myList:
                    if s in i:
                        i.remove(s)
print(myList)
['i', 'a', 'a', 'a']
print(matchList)
[['g', 'h'], ['b', 'c']]
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48
0

You could try a list comprehesion after iteration.

matchList = [['g', 'h', 'i'], ['a', 'b', 'c']]
first_list = ['i', 'am', 'an', 'old', 'man']
myList = []

for i in matchList:
    for j in i:
        for word in first_list:
            if j in word:
                myList.append(j)

# Remove items from sublists
matchList = [[j for j in matchList[i] if j not in myList] for i in range(len(matchList))]

Output:

In [7]: myList
Out[7]: ['i', 'a', 'a', 'a']

In [8]: matchList
Out[8]: [['g', 'h'], ['b', 'c']]
estebanpdl
  • 1,213
  • 1
  • 12
  • 31