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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:07:57+00:00 2026-05-18T20:07:57+00:00

Really two questions: If I have a dictionary (that originally came from parsing a

  • 0

Really two questions: If I have a dictionary (that originally came from parsing a json message) that has an optional array in it:

dict_with = {'name':'bob','city':'san francisco','kids': {'name': 'alice'} }
dict_without = {'name':'bob','city':'san francisco' }

I would normally have code like:

if 'kids' in dict:
   for k in dict['kids']:
      #do stuff

My first question is there any python way to combine the if protection and the for loop?

The second question is my gut tells me the better design for the original json message would be to always specify the kids element, just with an empty dictionary:

dict_better = {'name':'bob','city':'san francisco','kids': {} }

I can’t find any design methodology that substantiates this. The json message is a state message from a web service that supports json and xml representations. Since they started with xml, they made it so the “kids” element was optional, which forces the construct above of checking to see if the element exists before iterating over the array. I would like to know if it’s better design-wise to say the element is required (just with an empty array if there are no elements).

  • 1 1 Answer
  • 3 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-18T20:07:58+00:00Added an answer on May 18, 2026 at 8:07 pm
    for x in d.get("kids", ()):
        print "kid:", x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Really two questions: If I have a dictionary (that originally came from parsing a
I have two questions... Here is a really simple example script which causes an
I have a really simple class with two methods; One that will be called
This is really two questions, noted below: Currently I have some public internal helper
I guess I have two questions really. 1) I was wondering if anyone knows
I have two questions. 1) What does a constructor really do? What happens if
I have a dictionary of values read from two fields in a database: a
I'm really new to JavaScript and jQuery. My question is this. I have two
This is really two questions: Why isn't the membership operator (__contains__) ever being called?
Two questions, really: Is there a standard/convention regarding the placement on configuration files? For

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.