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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:11:03+00:00 2026-05-17T02:11:03+00:00

I have an lxml.objectify data structure I get from a RESTful web service. I

  • 0

I have an lxml.objectify data structure I get from a RESTful web service. I need to change a setting if it exists and create it if it doesn’t. Right now I have something along the lines of the following, but I feel like it’s ugly. The structure I’m looking in has a list of subelements which all have the same structure, so I can’t just look for a specific tag unfortunately.

thing_structure = lxml.objectify(get_from_REST_service())
found_thing = False
if thing_structure.find('settings') is not None:
    for i, foo in enumerate(thing_structure.settings):
        if foo.is_what_I_want:
            modify(thing_structure.settings[i])
            found_thing = True
if not found_thing:
    new = lxml.etree.SubElement(thing_structure, 'setting')
    modify(new)

send_to_REST_service(thing_structure)
  • 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-05-17T02:11:04+00:00Added an answer on May 17, 2026 at 2:11 am

    Overall, the structure isn’t too bad (assuming you need to call modify on 1+ items in the settings — if “just one”, i.e., if the is_what_I_want flag is going to be set for one setting at most, that’s of course different, as you could and should use a break from the for loop — but that’s not the impression of your intentions that I get from your Q, please clarify if I’ve mistead!). There’s one redundancy:

    for i, foo in enumerate(thing_structure.settings):
        if foo.is_what_I_want:
            modify(thing_structure.settings[i])
            found_thing = True
    

    Having i and using it to get again the same foo is no use here, so you can simplify to:

    for foo in thing_structure.settings:
        if foo.is_what_I_want:
            modify(foo)
            found_thing = True
    

    You’d only need the index if you were to rebind the item, i.e., perform an assignment such as thing_structure.settings = whatever. (BTW, a name other than foo wouldn’t hurt;-).

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

Sidebar

Related Questions

I am using lxml to get a string from a web page. What do
I have a web-app and initially I used feedparser to pull data from my
Here's the code I have: from cStringIO import StringIO from lxml import etree xml
I have been struggling to be able to do from lxml import etree (
Suppose I have this sort of HTML from which I need to select text2
I have this script- import lxml from lxml.cssselect import CSSSelector from lxml.etree import fromstring
I have this script: import urllib2 from BeautifulSoup import BeautifulSoup import html5lib import lxml
I have a small issue. I am trying to pull some data from my
I'm using lxml to parse and objectify xml files in a path, I have
I have some lxml element: >> lxml_element.text 'hello BREAK world' I need to replace

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.