I am recursing through an xml file, using etree.
import xml.etree.ElementTree as etree
tree = etree.parse('x.xml')
root = tree.getroot()
for child in root[0]:
 for child in child.getchildren():
        for child in child.getchildren():
            for child in child.getchildren():
               print(child.attrib)
what is the idiomatic way in python to avoid these nested for loop.
  getchildren() ⇒ list of Element instances [#]
    Returns all subelements. The elements are returned in document order.
Returns:
A list of subelements.
I saw some post in SO like, Avoiding nested for loops but doesn't directly translate to my use.
thanks.
If you want to get the children that are n levels deep in the tree, and then iterate through them, you can do:
def childrenAtLevel(tree, n):
    if n == 1:
        for child in tree.getchildren():
            yield child
    else:
        for child in tree.getchildren():
            for e in childrenAtLevel(child, n-1):
                yield e
Then, to get the elements four levels deep, you would simply say:
for e in childrenAtLevel(root, 4):
     # do something with e
Or, if you want to get all of the leaf nodes (i.e. the nodes that don't have any children themselves), you can do:
def getLeafNodes(tree):
    if len(tree) == 0:
         yield tree
    else:
         for child in tree.getchildren():
            for leaf in getLeafNodes(child):
                yield leaf
                        itertools.chain.from_iterable will flatten one level of nesting; you can use functools.reduce to apply it n times (Compressing "n"-time object member call):
from itertools import chain
from functools import reduce
for child in reduce(lambda x, _: chain.from_iterable(x), range(3), root):
    print(child.attrib)
Note that getchildren is deprecated; iterating a node yields its children directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With