title: How to conditionally insert an attribute into a node in Python using xml.etree.ElementTree
body: <p>I'm building an XML document using Python's xml.etree.ElementTree and am unable to conditionally insert an attribute into a node. Here's an example of my code:</p>
  
  <pre><code>Attr1="stamps"
  Attr2="ghouls"
  Attr3=""   
  
  node = ET.SubElement(root_node, "ChildNode1",
                           Attr1=Attr1,
                           Attr2=Attr2,
                           Attr3=Attr3)
  </code></pre>
  
  <p>Simple enough, output is exactly as expected. However, many XML documents I've seen exclude the attribute entirely if the value is None. How do I do this? Something like:</p>
  
  <pre><code>Attr1="stamps"
  Attr2="ghouls"
  Attr3=""   
  
  node = ET.SubElement(root_node, "ChildNode1",
                           Attr1=Attr1,
                           Attr2=Attr2,
                           (Attr3=Attr3 if Attr != "" else pass))
  </code></pre>
  
  <p>But this syntax is not supported by the library. I know I can do an if statement and copy the code over for the entire node minus the conditional value, but I do not want to duplicate code many times over. Is there a quick and easy way to do this using the ElementTree library?</p>