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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:59:28+00:00 2026-05-28T18:59:28+00:00

I usually use the following idiom when working with a Python dictionary: try: val

  • 0

I usually use the following idiom when working with a Python dictionary:

try:
    val = dct[key]
except KeyError:
    print key, " is not valid"

since for large dictionaries, the statement

if key in dct:
    # do something

is not very efficient (so I remember reading, but I’ve noticed it in practice as well)

Today I was working with a defaultdict and for a moment I forgot that a defaultdict will never give you a KeyError but instead will update the original dictionary.

How can I perform a lookup without updating the defaultdict? I really need to print an error so that the user can reenter the key.

Thank you!

UPDATE: Several posters suggested that my belief that if key in dct: is slow is false. I went back and checked the book in which I had read that is better to use try: except:. It is 2002’s Python Cookbook, Recipe 1.4 by Alex Martelli, which can be found also online here: Add an entry to dictionary. Old memories are so unreliable! The recipe doesn’t mention “slower” and it’s not even using in but has_key. It simply says that try: except: is more Pythonic (at least the book version of the recipe). Thanks for the correction and the answers.

  • 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-28T18:59:29+00:00Added an answer on May 28, 2026 at 6:59 pm

    How can I perform a lookup without updating the defaultdict?

    With key in dct, i.e. explicitly.

    If this is really too expensive for you (measure and you’ll be sure), there are workarounds for specific situations. E.g., if your default value is 'ham' and in some situations you don’t want to store (key, 'ham') in the defaultdict when key is not found, you can do

    dct.get(key, 'ham')  # will return dct[key] or 'ham' but never stores anything
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I usually use the following Python code to read lines from a file :
In our code we usually use the following pattern: Connection conn; try{ conn =
When working with 3d graphics, sample shaders USUALLY use the following operation for vector
I usually use the following pipeline to grep for a particular search string and
I usually use pointers in the following manner char *ptr = malloc( sizeof(char) *
I usually do not use Sql Server Management Studio, I usually use Linqpad to
I'd like to use the following idiom, that I think is non-standard. I have
The following script is what I usually use to push headers to the browser
So the branching model I usually use is similar to the following: http://nvie.com/posts/a-successful-git-branching-model/ However,
I'm just wondering why we usually use logical OR || between two booleans not

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.