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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:48:31+00:00 2026-05-26T08:48:31+00:00

I have got a project, where I have to do this very frequently: if

  • 0

I have got a project, where I have to do this very frequently:

if "something" in dict:
    some_var = dict["something"]
    del dict["something"]
else:
    error_handler_or_other_stuff()

However I have the idea to use this:

try:
    some_var = dict.pop("something")
except KeyError:
    error_handler_or_other_stuff()

My question is: in general, how “fast” try – except constructs are to handle exceptions? Is it OK to use it a lot, or it’s still faster to do the stuff “manually”. Also sometimes I have the situation where I have to convert value to integer:

try:
    some_var = int(dict.pop("something"))
except KeyError:
    error_handler_or_other_stuff("no such key")
except ValueError:
    error_handler_or_other_stuff("bad value for key")

Now the solution with exceptions seems to be quite nice, since I can do both of the checks in one step, also I removed the original key/value pair from dict, which is part of the problem. So I can tell at least: it looks like an elegant solution. However I am not sure it’s faster or if it has other disadvantages I should worry about.

  • 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-26T08:48:31+00:00Added an answer on May 26, 2026 at 8:48 am

    These kind of performance questions are easily answered using the timeit module:

    setup = '''
    d = {'a': 1}
    k = 'b'
    '''
    
    LBYL = '''
    if k in d:
        pass
    else:
        pass
    '''
    
    EAPF = '''
    try:
        d[k]
    except KeyError:
        pass
    '''
    
    from timeit import Timer
    
    print min(Timer(LBYL, setup).repeat(7, 1000000))
    print min(Timer(EAPF, setup).repeat(7, 1000000))
    

    The results show 0.0546 for the if/else approach and 1.3370 for the try/except approach. The latter is about 25 times slower than the former.

    That being said, you should generally use whatever expresses the clearest code.

    Sidenote: the two approaches give different answers for subclasses of dict that define missing to return a value.

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

Sidebar

Related Questions

I have this very strange issue with my MVC 2 project. Often times, I'll
I have finally got the green light to use Memcached in our project (after
I have got a project that is using jni to connect java wrapper and
I am a fresher and I have got a project in asp.net and c#.
I'm new in eclipse and I have got a project that can add and
I've got question to you. I have got two project in PHP that uses
Suppose I have got a model ArticleVersion in my project which is defined as:
In the project I'm working on I have got a list List<Item> with objects
I am designing an online accounting system as a college project. I have got
I got a Project from my Company without Documentation. Now I have to add

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.