Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:11:44+00:00 2026-06-14T14:11:44+00:00

I’m using ElementTree findall() to find elements in my XML which have a certain

  • 0

I’m using ElementTree findall() to find elements in my XML which have a certain tag. I want to turn the result into a list. At the moment, I’m iterating through the elements, picking out the .text for each element, and appending to the list. I’m sure there’s a more elegant way of doing this.

#!/usr/bin/python2.7
#
from xml.etree import ElementTree
import os
myXML = '''<root>
<project project_name="my_big_project">
<event name="my_first_event">
<location>London</location>
<location>Dublin</location>
<location>New York</location>
<month>January</month>
<year>2013</year>
</event>
</project>
</root>
'''

tree = ElementTree.fromstring(myXML)
for node in tree.findall('.//project'):
  for element in node.findall('event'):
    event_name=element.attrib.get('name')
    print event_name
    locations = []
    if element.find('location') is not None:
      for events in element.findall('location'):
        locations.append(events.text)
# Could I use something like this instead?
#      locations.append(''.join.text(*events) for events in element.findall('location'))

print locations

Outputs this (which is correct, but I’d like to assign the findall() results directly to a list, in text format, if possible;

my_first_event
['London', 'Dublin', 'New York']
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:11:45+00:00Added an answer on June 14, 2026 at 2:11 pm

    You can try this – it uses a list comprehension to generate the list without having to create a blank one and then append.

    if element.find('location') is not None:
      locations = [events.text for events in element.findall('location')]
    

    With this, you can also get rid of the locations definition above, so your code would be:

    tree = ElementTree.fromstring(myXML)
    for node in tree.findall('.//project'):
      for element in node.findall('event'):
        event_name=element.attrib.get('name')
        print event_name
        if element.find('location') is not None:
          locations = [events.text for events in element.findall('location')]
    
    print locations
    

    One thing you will want to be wary of is what you are doing with locations – it won’t be defined if location doesn’t exist, so you will get a NameError if you try to print it and it doesn’t exist. If that is an issue, you can retain the locations = [] definition – if the matching element isn’t found, the result will just be an empty list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I have an autohotkey script which looks up a word in a bilingual dictionary
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.