I have 2 lists. The list named "keyword" is a list I manually created, and the nested list named "mylist" is an output of a function that I have in my script. This is what they look like:
keyword = ["Physics", "Spanish", ...]
mylist = [("Jack","Math and Physics"),
("Bob","English"),
("Emily","Physics"),
("Mark","Gym and Spanish"),
("Brian", "Math and Gym"),
...]
What I am trying to do is delete each item in the nested list if that item (in parenthesis) contains any of the keywords written in the "keyword" list.
For example, in this case, any items in "mylist" that contain the words "Physics" or "Spanish" should be deleted from "mylist". Then, when I print "mylist", this should be the output:
[("Bob","English"), ("Brian", "Math and Gym")]
I tried searching through the internet and many different SO posts to learn how to do this (such as this), but when I modify (because I have a nested list, instead of just a list) the code and run it, I get the following error:
Traceback (most recent call last):
File "namelist.py", line 165, in <module>
asyncio.get_event_loop().run_until_complete(request1())
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 576, in run_until_complete
return future.result()
File "namelist.py", line 154, in request1
mylist.remove(a)
ValueError: list.remove(x): x not in list
Does anyone know how to fix this error, and could you share your code?
EDIT: By the way, the real "mylist" I have on my script is much longer than what I wrote here, and I have about 15 keywords. When I run it on a small scale like this, the code works well, but as soon as I have more than 5 keywords, for some reason, I keep getting this error.