Here's how I solved it:
#gives you a list of every parent,child tuple
def iterparent(tree):
for parent in tree.getiterator():
for child in parent:
yield parent, child
#recursive function. Deletes the given child node, from n parents back.
#If n = 0 it deletes just the child.
def removeParent(root, childToRemove, n):
for parent, child in iterparent(root):
if (childToRemove == child):
if n>0:
removeParent(root, parent, n-1)
else:
parent.remove(child)
for parent, child in iterparent(root):
if (child.tag == 'value' and (child.text in valuesToDelete):
if (parent.tag == 'list'):
removeParent(root, child, 0)
else:
removeParent(root, child, 2)
It's actually quite elegant. I like it.
For my purposes, this works well, but one might have difficulty with a wide range of element structures and depths.