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 8900495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:06:39+00:00 2026-06-15T01:06:39+00:00

I have problem with dictionaries within python, I am currently getting what information I

  • 0

I have problem with dictionaries within python, I am currently getting what information I want out of my incoming xml thanks to “Namit Kewat”. And when I print the output it lists all of the things it found, each in its own dictionary, which is good.

However when I try to for loop over the “output” dictionary to find the all of the “Active” keys and the values they contain. It only returns one value, and that is the value of the last “Active” found within the schema.

So my problem is, how can I iterate or for loop etc, over ALL these dictionaries. I want the dictionary to be called “output” and there will be many “AssetEquipment” sections in my incoming xml. If dictionaries are not the way then please advise better solutions. Essentially my goal is to iterate of many “AssetEquipment” to get values and then extend that to cover other things within the xml file like “AssetSupport”. So there is a lot of groups with multiple versions/instances that need.

Thank you.

import xml.etree.cElementTree as ET
tree = ET.parse('test.xml')
for elem in tree.getiterator():
    if elem.tag=='{http://www.namespace.co.uk}AssetEquipment':
        output={}
        for elem1 in list(elem):
            if elem1.tag=='{http://www.namespace.co.uk}Active':
                output['Active']=elem1.text
            if elem1.tag=='{http://www.namespace.co.uk}Direction':
                output['Direction']=elem1.text
            if elem1.tag=='{http://www.namespace.co.uk}Location':
                for elem2 in list(elem1):
                    if elem2.tag=='{http://www.namespace.co.uk}RoomLocation':
                        for elem3 in list(elem2):
                            if elem3.tag=='{http://www.namespace.co.uk}Room':
                                output['Room']=elem3.text
        print output

Sample Input (Kept it small as it is too large to post everything):

<AssetEquipment>
    <Name>PC123</Name>
    <Active>Yes</Active>
    <Direction>Positive</Direction>
    <Location>
        <RoomLocation>
            <Room>18</Room>
        </RoomLocation>
    </Location>
</AssetEquipment>
<AssetEquipment>
    <Name>PC256</Name>
    <Active>No</Active>
    <Direction>Positive</Direction>
    <Location>
        <RoomLocation>
            <Room>19</Room>
        </RoomLocation>
    </Location>
</AssetEquipment>

Sample Output,
Via Print:

{'Direction': 'Positive', 'Active': 'Yes', 'Room': '18'}
{'Direction': 'Positive', 'Active': 'No', 'Room': '19'}

Via for loop:

def isactive():
    for key in output:
        print output.get("Active")

No
No

Desired Output:

Yes
No
  • 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-15T01:06:40+00:00Added an answer on June 15, 2026 at 1:06 am

    Two problems:

    1. You’re overwriting the output dictionary for each AssetEquipment. It works with the inline print statement, but you can’t loop over the results later. You should save each output dict in a list.

      results = []
      for elem in tree.getiterator():
          if elem.tag=='{http://www.namespace.co.uk}AssetEquipment':
              output={}
              results.append(output)
              ...
      
    2. You need to loop through the result list, not over the keys of a single output dict

      def isactive():
          for output in results:
              print output.get("Active")
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my problem: I have a list of Python dictionaries of identical form,
Here's my problem: Lets say I have two dictionaries, dict_a and dict_b. Each of
I have a problem with combining or calculating common/equal part of these two dictionaries.
I am new to Python, and I am currently stumped by this problem: I
I'm experiencing a (for me) very weird problem in Python. I have a class
first post, so play nice! I have a fairly basic question about Python dictionaries.
i have a problem with Resource dictionaries and mergeddictionaries in general, especially when it
I've been having a problem with making sorted lists from dictionaries. I have this
I have an array of dictionaries. I want to filter the array based on
I am new to tableViews and dictionaries and i have a problem! In ViewDidLoad

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.