[lxml-dev] Bug in objectify node[:].index

Stefan Behnel stefan_ml at behnel.de
Thu May 24 16:36:44 CEST 2007


Hi,

I only noticed now that your paragraph below was a) well hidden and b) left
unanswered.

Christian Zagrodnick wrote:
> Anyway, all I *actually* want is to remove the nodes found by the 
> xpath. The way you'd think it would be 'normal' doesn't work 
> unfortunately:
> 
> (Pdb) p parent.index(foo_tree)
> 2
> (Pdb) del parent[2]
> *** TypeError: deleting items not supported by root element

Sounds pretty inefficient to me, it even has to traverse the children twice.
The following is usually much faster, clearer and also works in this case:

  parent = foo_tree.getparent()
  if parent is not None:
      parent.remove(foo_tree)

Stefan



More information about the lxml-dev mailing list