Am June 23, 2022 11:20:59 PM UTC schrieb Parfait G <parfait.gas...@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 _______________________________________________ lxml - The Python XML Toolkit mailing list -- lxml@python.org To unsubscribe send an email to lxml-le...@python.org https://mail.python.org/mailman3/lists/lxml.python.org/ Member address: arch...@mail-archive.com