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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:37:42+00:00 2026-06-13T01:37:42+00:00

If I set a default value on a dict, is it possible to get

  • 0

If I set a default value on a dict, is it possible to get the default value back after its been overwritten?

Like this:

dd = {}
dd.setdefault('beverage':'A nice cup of tea')
...
dd['beverage'] = 'Yellow urine'
...
if dd['beverage'] not in list_of_valid_beverages:
    # If the beverage has been contaminated revert to default.
    # Something like any of the following?
    dd['beverage'] = dd.getdefault('beverage')
    dd['beverage'].clear()
    del dd['beverage']
...
print dd['beverage']

My guess is that this is not possible with an ordinary dict. Ones the statement

dd[‘beverage’] = ‘Yellow urine’

is executed the tea is forever lost. But it would be a nice feature 🙂

Anyone with deeper knowledge about this?

  • 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-13T01:37:43+00:00Added an answer on June 13, 2026 at 1:37 am

    No, it’s not possible. All setdefault does is the following logic:

    if key not in the_dict:
        the_dict[key] = value
    return the_dict[key]
    

    It does not treat the “default” value specially, and doesn’t store it anywhere other than the location that you’re overwriting.

    If you want a set of persistent defaults, you’re best off just keeping a separate dict of the default values.

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

Sidebar

Related Questions

In mysql I can set default value for column like this: ALTER TABLE <Table>
THIS QUESTION IS NOT ABOUT HOW TO SET DEFAULT VALUE OF A WIDGET Hello
I know you can use setdefault(key, value) to set default value for a given
Hi I would like set default value for binding on Text property. In widows
How can I set default value of Who can see posts this app makes
I would like to know how to set default value on form_for select. My
how can i set default value in sonata admin bundle the data option is
How to set default VALUE in SubQuery result using mysql SELECT p.`id`, p.`name`, p.`class_name`,
i want to set the default value in a sql server 2005 datetime field
When I set a default value formula for a date parameter in SSRS, such

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.