title: How to delete a node from an XML document in Python using ElementTree
body: <p>Here's the structure: </p>
  
  <pre><code>   &lt;foo&gt;
          &lt;bar&gt; 
      &lt;buildCommand&gt;
          &lt;name&gt;com.android.ide.eclipse.adt.ApkBuilder&lt;/name&gt;
          &lt;arguments&gt;
          &lt;/arguments&gt;
      &lt;/buildCommand&gt;
      &lt;buildCommand&gt;
          &lt;name&gt;org.eclipse.ui.externaltools.ExternalToolBuilder&lt;/name&gt;
          &lt;triggers&gt;auto,full,incremental,&lt;/triggers&gt;
      &lt;/buildCommand&gt;
          &lt;/bar&gt;
     &lt;/foo&gt;
  </code></pre>
  
  <p>and here's my logic, which identifies the buildCommand I want to delete (the second one), adds it to a list, and then does a remove. </p>
  
  <pre><code>import os;
  import xml.etree.ElementTree as ET
  
  document = ET.parse("foo"); 
  root = document.getroot(); 
  removeList = list()
  for child in root.iter('buildCommand'): 
     if (child.tag == 'buildCommand'): 
        name = child.find('name').text
        if (name == 'org.eclipse.ui.externaltools.ExternalToolBuilder'):
            removeList.append(child)
  
  for tag in removeList:
     root.remove(tag)
  
  document.write("newfoo")
  </code></pre>
  
  <p>Python 2.7.1 has the remove command but I get an error on remove: </p>
  
  <p>File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 337, in remove
      self._children.remove(element)
  ValueError: list.remove(x): x not in list</p>
  
  <p>UPDATE: </p>
  
  <p><em>*</em> Solved by @martijn-pieters - correct logic for second for loop is </p>
  
  <pre><code>for tag in removeList:
     parent = root.find('bar')
     parent.remove(tag)
  </code></pre>