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

  • Home
  • SEARCH
  • 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 6588851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:05:58+00:00 2026-05-25T17:05:58+00:00

I am trying to create a new dictionary out of html form data that

  • 0

I am trying to create a new dictionary out of html form data that was submitted by the user. I end up writing repetitive if statements, checking if xyz key is in the dictionary in the form data. I know this is a quite suboptimal approach though I am not quite sure how to implement this using python.

This is the form data dictionary:

form_data = {
'urls': ['www.google.com', 'www.bing.com'],
'useremail': ['my@email.com'],
'emailfield': ['1'],
'addressfield': ['1'],
'addressfield_info':['Company'],
'addressfield_instruction': ['Please only if the company is a LLC'],
'phonefield': ['1'],
'phonefield_instruction': ['please include area code']
}

and I want to create a dictionary that looks like this:

new_dic = {
'urls': ['www.google.com', 'www.bing.com'],
'useremail': ['my@email.com'],
'infofield': [
              {'field': 'email'},
              {'field': 'address', 'info':'Company', 'instruction': 'Please only if the company is a LLC'},
              {'field':'phone', 'instruction': 'please include area code'}
             ]
}

Important note: The ‘xyzfield’ is mandatory and the ‘xyzfield_info’ and ‘xyzfield_instruction’ are both optional. Also: the user can add more fields and create for instance an ‘agefield’, ‘agefield_info’ and ‘agefield_instruction’.

The problem I have is about how to efficiently check if xyzfield (email, phone, etc) is in the dictionary. If it is in there, check also if any of the optional fields are in there as well. This looks currently something like this:

if 'emailfield' in form_data:
    infofield = {'field': 'email'}
    if 'emailfield_info' in form_data:
         infofield['info'] = form_data['emailfield_info']
    if 'emailfield_instruction' in form_data:
         infofield['instruction'] = form_data['emailfield_instruction']

cleaned_data['infofields'].append(infofield)

...

and I do this for every field, hence I have 4-5 of this. Additional, I will not be able to process any of the fields that the user has created himself since I don’t know the name upfront.

Long story short: How can I make this more efficient and dynamic?

  • 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-25T17:05:59+00:00Added an answer on May 25, 2026 at 5:05 pm

    The standard answer to how to avoid repeated code applies here — extract the repeated code to a function:

    def extract_field(form_data, clean, fieldname, optional=('info', 'instruction')):
       if fieldname+'field' in form_data:
           infofield = { 'field': fieldname }
           for opt in optional:
               optname = '{}field_{}'.format(fieldname, opt)
               if optname in form_data:
                  infofield[opt] = form_data[optname]
           clean.append(infofield)
    
    extract_field(form_data, cleaned_data['infofields'], 'email')
    extract_field(form_data, cleaned_data['infofields'], 'address')
    extract_field(form_data, cleaned_data['infofields'], 'phone')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a new variable that will consist of an existing dictionary
I'm trying to use LINQ to create a new dictionary from an existing one,
I am trying to create new Event objects to be persisted in the database
I'm trying to create a new Excel file using jxl, but am having a
I am trying to create a new contact using Dynamic Entity. The sample i
I am trying to create a new website on a remote server via msbuild
I'm trying to create a new module for Sitefinity. I'm basing my module off
I'm trying to create a new object of type T via its constructor when
I'm trying to create a new project in NetBeans PHP from existing sources. When
I am trying to create a new dict using a list of values of

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.