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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:03:45+00:00 2026-05-23T23:03:45+00:00

In my Ruby application I have a hash table: c = {:sample => 1,:another

  • 0

In my Ruby application I have a hash table:

c = {:sample => 1,:another => 2}

I can handle the table like this:

[c[:sample].nil? , c[:another].nil? ,c[:not_in_list].nil?]

I’m trying to do the same thing in Python. I created a new dictionary:

c = {"sample":1, "another":2}

I couldn’t handle the nil value exception for:

c["not-in-dictionary"]

I tried this:

c[:not_in_dictionery] is not None

and it is returning an exception instead of False. How do I handle 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-05-23T23:03:46+00:00Added an answer on May 23, 2026 at 11:03 pm

    In your particular case, you should probably do this instead of comparing with None:

    "not_in_dictionary" in c
    

    If you were literally using this code, it will not work:

    c[:not_in_dictionary] is not None
    

    Python doesn’t have special :keywords for dictionary keys; ordinary strings are used instead.


    The ordinary behaviour in Python is to raise an exception when you request a missing key, and let you handle the exception.

    d = {"a": 2, "c": 3}
    
    try:
        print d["b"]
    except KeyError:
        print "There is no b in our dict!"
    

    If you want to get None if a value is missing you can use the dict‘s .get method to return a value (None by default) if the key is missing.

    print d.get("a") # prints 2
    print d.get("b") # prints None
    print d.get("b", 0) # prints 0
    

    To just check if a key has a value in a dict, use the in or not in keywords.

    print "a" in d # True
    print "b" in d # False
    print "c" not in d # False
    print "d" not in d # True
    

    Python includes a module that allows you to define dictionaries that return a default value instead of an error when used normally: collections.defaultdict. You could use it like this:

    import collections
    
    d = collections.defaultdict(lambda: None)
    print "b" in d # False
    print d["b"] # None
    print d["b"] == None # True
    print "b" in d # True
    

    Notice the confusing behaviour with in. When you look up a key for the first time, it adds it pointing to the default value, so it’s now considered to be in the dict.

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

Sidebar

Related Questions

I have written a Ruby application which parses lots of data from sources in
I want to run javascript/Python/Ruby inside my application. I have an application that creates
How can I find out, which keyboard layout the user of my ruby application
I have a ruby on rails application.I have 2 drop down boxes.I have to
I developed a simple Ruby Hello World application. Now i would like to consume
I have been using ActiveResource in my Ruby on Rails applications for some time,
Suppose you have an ActiveRecord::Observer in one of your Ruby on Rails applications -
I want to send email from a Ruby application. Is there a call in
Is it possible to run a ruby application as a Windows Service? I see
After our Ruby on Rails application has run for a while, it starts throwing

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.