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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:05:55+00:00 2026-05-23T08:05:55+00:00

The default behavior for python dictionary is to create a new key in the

  • 0

The default behavior for python dictionary is to create a new key in the dictionary if that key does not already exist. For example:

d = {}
d['did not exist before'] = 'now it does'

this is all well and good for most purposes, but what if I’d like python to do nothing if the key isn’t already in the dictionary. In my situation:

for x in exceptions:
    if masterlist.has_key(x):
        masterlist[x] = False

in other words, i don’t want some incorrect elements in exceptions to corrupt my masterlist. Is this as simple as it gets? it FEELS like I should be able to do this in one line inside the for loop (i.e., without explicitly checking that x is a key of masterlist)

UPDATE:
To me, my question is asking about the lack of a parallel between a list and a dict. For example:

l = []
l[0] = 2 #fails
l.append(2) #works

with the subclassing answer, you could modify the dictionary (maybe “safe_dict” or “explicit_dict” to do something similar:

d = {}
d['a'] = '1' #would fail in my world
d.insert('a','1') #what my world is missing
  • 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-23T08:05:56+00:00Added an answer on May 23, 2026 at 8:05 am

    You can inherit a dict class, override it’s __setitem__ to check for existance of key (or do the same with monkey-patching only one instance).

    Sample class:

    class a(dict):
        def __init__(self, *args, **kwargs):
            dict.__init__(self, *args, **kwargs)
            dict.__setitem__(self, 'a', 'b')
    
        def __setitem__(self, key, value):
            if self.has_key(key):
              dict.__setitem__(self, key, value)
    
    a = a()
    print a['a'] # prints 'b'
    a['c'] = 'd'
    # print a['c'] - would fail
    a['a'] = 'e'
    print a['a'] # prints 'e'
    

    You could also use some function to make setting values without checking for existence simpler.
    However, I though it would be shorter… Don’t use it unless you need it in many places.

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

Sidebar

Related Questions

I know that to alter the default behavior of operators in python you can
It is my understanding that the default behavior when creating a table in SQL
In JQuery validation the default behavior on an error is to create a label
In Python it's possible to create a procedure that has no explicit return. i.e.:
I often see python code that takes default arguments and has special behaviour when
The default behavior of XML serialization (to_xml) for ActiveRecord objects will emit 'type' and
We'd like to override DataGridView's default behavior when using a mouse wheel with this
I would like to change the default behavior of Joomla's password reminding mechanism. I
I would like to override the default behavior of the offsetParent member of all
Rails question. The default behavior for error validation is to fieldWithError-styled div around the

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.