![](https://secure.gravatar.com/avatar/8b97b5aad24c30e4a1357b38cc39aeaa.jpg?s=120&d=mm&r=g)
26 Jun
2022
26 Jun
'22
7:02 a.m.
Am June 23, 2022 11:20:59 PM UTC schrieb Parfait G <parfait.gasana@gmail.com>:
I see one fix is to also check if `elem.getparent() is not None`. Thoughts?
elem.clear() while elem.getprevious() is not None and elem.getparent() is not None: del elem.getparent()[0]
The parent won't change during the loop, so it's enough to check it once before the loop. Also, there is only one element without parent, that's the root element. Maybe you can skip that altogether in your processing? It should be the first item returned by the iterator that you got through .iter(). Just call next() on it once. Stefan