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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:15:58+00:00 2026-06-06T03:15:58+00:00

I have a bit of experience in Python but I’m taking the Udacity computer

  • 0

I have a bit of experience in Python but I’m taking the Udacity computer science course to fill in the gaps of what I’ve learned and to supplement what I already know. The course went over a hashtable lookup function that returns None for the value of a key if the key isn’t in the hashtable. Python’s dictionary type throws a KeyError when the key doesn’t exist, so the course says to use key in mydict before getting its value.

What I’m wondering is if it’s better to do:

mydefaultval = 75
key = ..
mydict = ..
if key in mydict:
    val = mydict[key]
else:
    val = mydefaultval

.. or

mydefaultval = 75
key = ..
mydict = ..
try:
    val = mydict[key]
except KeyError:
    val = mydefaultval

I would imagine that for Python to check if the key exists, it has to find it in the dictionary. If I then grab the value at that key, it has to do the same thing twice. Am I correct for thinking this, or will Python do something else?

  • 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-06T03:16:00+00:00Added an answer on June 6, 2026 at 3:16 am

    The best thing is to use dict.get:

    val = my_dict.get(key, mydefaultval)
    

    If there weren’t an awesome method like this, I’d recommend using the exception. Python culture is that exceptions are not forbidden, and often the explicit check will actually be catching exceptions anyway.

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

Sidebar

Related Questions

I have quite a bit experience in Python , but I haven't yet used
I am a bit new with android but I have some java experience. So
I have a fair bit of experience with PHP frameworks and Python for scripting
So I have a bit of experience building android applications. But now i would
Does anybody have any experience/ knowledge in installing the aspImage.dll on a 64-bit Windows
I have little bit longer question for you - but hope answer will be
I haven't worked with hibernate. I have little bit of experience in java. I
I have a bit of experience writing OpenGL 2 applications and want to learn
I have a bit of experience setting up online payment systems that accept credit
My first delve into working with JSON data. I have a bit of experience

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.