Possible Duplicate:
Remove items from a list while iterating in Python
I have a fairly embedded list: specifically, it is a list of lists of tuples. To simplify things, the entire list is a list of sentences. Within each sentence, each word is made into a tuple, containing information about that word. The last tuple in each sentence contains information about the speaker, but can be removed, if need be.
I'd like to search through these tuples, and, if a certain value is found, then remove the entire sentence.
Here is a sample list:
sentenceList = [[('the', 'det', '1|2|DET'), ('duck', 'n', '2|3|SUBJ'), ('xxx', 'unk', '3|0|ROOT'), ('*MOT', 373)],
[('yyy', 'unk', '1|0|ROOT'), ('*CHI', 375)],
[('what', 'pro', '1|2|OBJ'), ('happen-PAST', 'v', '2|0|ROOT'), ('to', 'prep', '3|2|JCT'), ('the', 'det', '4|5|DET'), ('duck', 'n', '5|3|POBJ'), ('*MOT', 378)],
[('boom', 'int', '1|0|ROOT'), ('*CHI', 379)]]
If a sentence contains either 'xxx'
or 'yyy'
, I'd like to remove the entire sentence. The code I tried was:
wordList = ['xxx','yyy']
for sentence in sentenceList:
for wordTuple in sentence:
for entry in wordTuple:
if entry in wordList:
del sentence
This should delete the entire sentence, i.e:
[('the', 'det', '1|2|DET'), ('duck', 'n', '2|3|SUBJ'), ('xxx', 'unk', '3|0|ROOT'), ('*MOT', 373)], [('yyy', 'unk', '1|0|ROOT'), ('*CHI', 375)]
However, this code doesn't seem to be accomplishing the task. Any idea how to fix it? Thanks!