-1

I'm trying to comparing two lists (mama and filterkeywords in the code shown), and remove values from the first list depending on the second. I have this code:

occurenceList = []
title = ['SL C ','FS D ','FS H','FS F','FS D','SL C','SL F','FS H','SL H','SL D']
mama = ['slow cat Sas','fast dog PoP','fast hen Luke','fast fish Lee','fast dog joe','slow cat yan','slow fish ben','Fast hen Tim','Slow hen Jen','slow dog moe']
filterkeywords = ['hen','dog','fish','Fish']

print(" ")
for text in mama:
   for words in filterkeywords:
      if words in text:
         animal = (mama.index(text))
         print("index: ", animal, ' | ', mama[animal], " | Word Found: ", words)
         title.pop(animal)
         mama.pop(animal)

print(" ")
for items in title:
   itemindex = title.index(items)
   print(itemindex, " ", items,' ',mama[itemindex])

Which gives this output:

index:  1  |  fast dog PoP  | Word Found:  dog
index:  2  |  fast fish Lee  | Word Found:  fish
index:  4  |  slow fish ben  | Word Found:  fish
index:  5  |  Slow hen Jen  | Word Found:  hen
 
0   SL C    slow cat Sas
1   FS H   fast hen Luke
2   FS D   fast dog joe
3   SL C   slow cat yan
1   FS H   fast hen Luke
5   SL D   slow dog moe

The nested for-loop appears to find only some of the matches, and is not outputting as intended. Why does this happen? What is the best way to remove each item from mama that contains any of the filterkeywords?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
TechLlama
  • 3
  • 2
  • 1
    Does this answer your question? [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – ddejohn Jul 02 '22 at 20:32
  • 1
    I even tried to experiment with doing zips and sets to no avail – TechLlama Jul 02 '22 at 20:33
  • 1
    Welcome to Stack Overflow. " I tried to mutate a copy version of the lists but still does not work." We can only diagnose code that is actually shown to us. If you believe that that version of the code should fix the problem and it still doesn't work, then base the question off that. If you tried doing that because of existing advice (e.g. https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating), then make that clear. – Karl Knechtel Jul 02 '22 at 20:34
  • 1
    Please also read [ask] and [mre], and try to simplify the code to isolate the part that causes the problem. Note well that this is **not a discussion forum**. We don't *solve problems* here (i.e., get your code to work for the overall task you want the code to perform); we *answer questions* (i.e., explain what is wrong with a specific bit of the code or with what it's trying to do; illustrate specific techniques; etc.) – Karl Knechtel Jul 02 '22 at 20:36
  • Next time should I do shorter coder without the visualization and bigger picture of what I'm trying to get at? I assumed it would be easier to understand with the prints @KarlKnechtel – TechLlama Jul 02 '22 at 20:44
  • We can most easily understand by seeing: 1) the *exact* input *as a data structure*; 2) the *exact* desired and actual outputs, also as structured data rather than custom-formatted output. If you want to make it e.g. print on multiple lines and show the internal structure of a nested dict/list using indentation, you can try the standard library `pprint` module. Also, if the question is specifically about removing stuff from a list, then we don't need *any* of the code about counting occurrences. You can [edit] the post, rather than waiting for "next time". – Karl Knechtel Jul 02 '22 at 21:05
  • There is a separate issue with the final output: `.index` finds the *first match* for something, and it must scan the entire list each time. It does not give you "the index" for iterating through a for loop. See https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops. – Karl Knechtel Jul 02 '22 at 21:25
  • Anyway, please try following the advice from https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating; if you're still stuck, ask a new question, showing the code that you tried after following that advice. "zips and sets" are not relevant here; you are not removing *duplicates of other values* from `mama` (`set` deduplication) and you don't want or need to pair up elements from two lists of the same length (`zip` iteration). – Karl Knechtel Jul 02 '22 at 21:26

1 Answers1

0

This is a very straightforward place to use a list comprehension to generate a list of items that do not contain any of those keywords.

>>> [f"{i}   {t}   {m}" for i, (t, m) in enumerate(zip(title, mama)) 
...                     if not any(w in m for w in filterkeywords)]
['0   SL C   slow cat Sas', '5   SL C   slow cat yan']
>>>

The generator expression passed to any does the work of checking if any of the keywords are present. We used zip to combine the titles and elements in mama. And enumerate provides the index.

Chris
  • 26,361
  • 5
  • 21
  • 42
  • Yeah that actually worked. And bonus is when i removed the pop from the other code, it's actually finding all the locations of the keywords. your code is making a new list without those keywords. I spent hours looking online but I haven't heard of list comprehension. I'll will look into and learn this. Thank you! – TechLlama Jul 03 '22 at 05:21