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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:49:10+00:00 2026-06-10T09:49:10+00:00

Basically this started with my problem that I had when trying to find if

  • 0

Basically this started with my problem that I had when trying to find if the index exist in a dict:

if collection[ key ]: # if exist
    #do this
else: # if no exist
    #do this

But when the index really doesn’t exist it throws me a KeyError.
So, reading the Python documentation. If the missing() is defined it will not throw the KeyError.

collection = {}
def collection.__missing__():
    return false

The above code on the terminal gives me:

ghelo@ghelo-Ubuntu:~/Music$ python __arrange__.py
  File "__arrange__.py", line 16
    def allArts.__missing__():
               ^
SyntaxError: invalid syntax

So, how to do this correctly?
Btw, I’ll be needing to use Python 2.7 on this. And is there a difference when running on Python 3?

  • 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-10T09:49:11+00:00Added an answer on June 10, 2026 at 9:49 am

    This is how you do it:

    if key in collection:
    

    or, as suggested by @sdolan, you can use the .get method, which returs a default (optional second parameter) if it does not exist.

    if collection.get(key, None):
    

    If you want to use __missing__ you would apply it to a class that extends dict (in this case):

    class collection(dict):
    
        def __missing__(self, key):
            print "Too bad, {key} does not exist".format(key=key)
            return None
    
    
    d = collection()
    d[1] = 'one'
    
    print d[1]
    
    if d[2]:
        print "Found it"
    

    OUTPUT

    one
    Too bad, 2 does not exist
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I though I had solved this problem, but it is back: Code generation for
I'm barely started to use Jenkins and this is the first problem I've had
Basically, my problem is that i need to send data from my service (started
Basically started with Squid and iptables today (google is your friend). This stuff is
Basically this is a question how to access local scope handler. I trying to
So basically this code was working fine before. I had some computer issues and
The criteria is basically this: folders will exist for about 24-48hrs folders names can
The problem is basically this, in python's gobject and gtk bindings. Assume we have
My understanding is that Ladner's theorem is basically this: P != NP implies that
I'm trying to make the so called fine tune thing. Basically this looks like:

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.