Thanks. I had created a list that I called 'garbage' rather than 'candidates', but I thought that the append action would move the element out of where it is. So I didn't run the second part of the cycle. The odd and not particularly intuitive behaviour of list elements in loops is perhaps worth a separate paragraph or subsection both in Stefan's documentation and in John Shipman's excellent tutorial. If I find the time I will draft a section,although a technically more competent person should sign off on it. It might have the title 'Beware of parent.remove' Martin Mueller Professor emeritus of English and Classics Northwestern University On 4/20/17, 16:16, "Jens Quade" <jq@qdevelop.de> wrote:
You can loop through text specifying different elements, as in
for tree.iter(tei + Œw¹, tei + Œpc¹): If this: do Œx¹ If that: do Œy¹
But this doesn¹t seem to work if the action involved is of the parent.remove(x) kind.
Removals can modify the list of elements iterated on, and the for loop behaviour is not defined in this case.
I tend to create a list of deletion candidates, and delete them after the detection loop through the tree.
candidates = []
for node in tree.iter(Š): if some_criterium(node): candidates.append(node)
for node in candidates: node.getparent().remove(node)