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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:31:35+00:00 2026-06-10T13:31:35+00:00

I am reading an element list from an xml file and make the data

  • 0

I am reading an element list from an xml file and make the data into 2 dictionaries.

Was this the fastest way? (I don’t think this is the best, you guys always surprise me.;-)

ADict = {}
BDict = {}
for x in fields:
    key = x.get('key')
    ADict[key] = x.find('A').text
    BDict[key] = x.find('B').text

I think add it one by one is a bad idea, but write it in a single line. aka more pythonic way like this

 ADict,BDict = [dict(k) for k in zip(*([(x.get('key'),x.find('A').text),(x.get('key'),x.find('B').text)] for x in fields))]

I don’t think it’s better, two reasons,
first, x.get('key') was called twice
second, create too much temp tuples

  • 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-10T13:31:36+00:00Added an answer on June 10, 2026 at 1:31 pm

    Not tested, but should work

    ADict = dict((x.get('key'), x.find('A').text) for x in fields)
    BDict = dict((x.get('key'), x.find('B').text) for x in fields)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a CSV dataset config element, which is reading from a file like
I'm reading an XML file that looks like this: <?xml version=1.0 encoding=utf-8?> <VehicleList> <Vehicle>
I want to create a list out of an xml file where only values
I have a program that takes a list of names from a file and
i've been reading from the file and i have hard time getting rid of
Hey, I've been working on this problem to read numbers from a text file
I have binary data in a file (a list of 32-bit integer values) that
reading the documentation for java org.w3c.dom.ls it seems as a Element only can be
I'm reading this basic tutorial on canvas elements . The (almost) in the following
Reading the docs, I'd expect $(#wrap2).remove(.error) to remove all .error elements from #wrap2 .

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.