def remove_blanks(elem):
 out = list()
 for child in elem:
  remove_blanks(child)
  if (child.text or (len(child) > 0)):
   out.append(child)
 elem[:] = out

def remove_blanks ( elem ) : out = list ( ) for child in elem : remove_blanks ( child ) if ( child . text or ( len ( child ) > 0 ) ) : out . append ( child ) elem [ : ] = out
remove all elements and subelements with no text and no child elements .