Finding data on XML using Python's LXML Using Python's LXML I must read an XML file and print from each "basic" and "expert" tag, the name and email text from it. I've done a script that works but I don't think is the best way of doing this. Is there a better (simpler) way for getting the data of the XML without having to make 2 iterations on it?

Python so far:

from lxml import etree

myXML = "data.xml"
tree = etree.parse(myXML)
root = tree.getroot()
for node in root:
    if node.tag == "basic" or node.tag == "expert":
        user = [None] * 4
        for i, child in enumerate(node):
            if child.tag == "name":
                user[0] = i
                user[1] = child.text
            if child.tag == "email":
                user[2] = i
                user[3] = child.text
        print user
        if user[3].startswith('_'):
            # do some other things with data if email begins with _ ...


Will print:

[0, 'f.bar', 1, 'foobar@me.com']
[0, 'm.bob', 3, 'm.bob@email.com']
[0, 'm.bab', 3, 'm.bab@email.com']


XML sample:



&lt;?xml version="1.0"?&gt;
&lt;users&gt;
    &lt;id&gt;11111&lt;/id&gt;
    &lt;checked&gt;True&lt;/checked&gt;
    &lt;version&gt;A12&lt;/mode&gt;
    &lt;basic&gt;
        &lt;name&gt;f.bar&lt;/name&gt;
        &lt;email&gt;foobar@me.com&lt;/email&gt;
        &lt;forename&gt;Foo&lt;/forename&gt;
        &lt;surname&gt;Bar&lt;/surname&gt;
    &lt;/basic&gt;
    &lt;expert&gt;
        &lt;name&gt;m.bob&lt;/name&gt;
        &lt;forename&gt;Mak&lt;/forename&gt;
        &lt;surname&gt;Bob&lt;/surname&gt;
        &lt;email&gt;m.bob@email.com&lt;/password&gt;
    &lt;/expert&gt;
    &lt;expert&gt;
        &lt;name&gt;m.bab&lt;/name&gt;
        &lt;forename&gt;Mak&lt;/forename&gt;
        &lt;surname&gt;Bab&lt;/surname&gt;
        &lt;email&gt;m.bab@email.com&lt;/password&gt;
    &lt;/expert&gt;
    &lt;guru&gt;
        &lt;name&gt;e.guru&lt;/name&gt;
        &lt;forename&gt;Nick&lt;/forename&gt;
        &lt;email&gt;nick@email.com&lt;/password&gt;
        &lt;surname&gt;Gru&lt;/surname&gt;
    &lt;/guru&gt;
&lt;/users&gt;
Currently, you are overlooking one of the advantages in using lxml with its fully compilant W3C XPath 1.0 (even XSLT 1.0) language modules. 

Right now, your code really follows the syntax of Python's built-in etree, without any xpath() calls that can run dynamic parsing off node names. 

Below iterates through all &lt;basic&gt; and &lt;expert&gt; tags and retrieves their child &lt;name&gt; and &lt;email&gt; all in one loop or list comprehension. And to retrieve their position we count their preceding siblings with count(preceding-sibling::*).

from lxml import etree

myXML = "data.xml"
tree = etree.parse(myXML)

user = []

# FOR LOOP
for i in tree.xpath("//*[name()='basic' or name()='expert']"):
    user.append([i.xpath("count(name/preceding-sibling::*)"), 
                 i.find("name").text, 
                 i.xpath("count(email/preceding-sibling::*)"), 
                 i.find("email").text])        
print(user)
# [[0.0, 'f.bar', 1.0, 'foobar@me.com'], 
#  [0.0, 'm.bob', 3.0, 'm.bob@email.com'], 
#  [0.0, 'm.bab', 3.0, 'm.bab@email.com']]


# LIST COMPREHENSION
user = [[i.xpath("count(name/preceding-sibling::*)"), 
         i.find("name").text, 
         i.xpath("count(email/preceding-sibling::*)"), 
         i.find("email").text] 
        for i in tree.xpath("//*[name()='basic' or name()='expert']")]

print(user)
# [[0.0, 'f.bar', 1.0, 'foobar@me.com'], 
#  [0.0, 'm.bob', 3.0, 'm.bob@email.com'], 
#  [0.0, 'm.bab', 3.0, 'm.bab@email.com']]