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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:54:22+00:00 2026-06-17T16:54:22+00:00

Is there a clear best practice for assigning a variable from a key/value pair

  • 0

Is there a clear best practice for assigning a variable from a key/value pair in a Python dictionary:

  • If the key is present
  • If the key’s value is not an empty string

And otherwise assigning a default value to the variable.

I would like to use dict.get:

my_value = dict.get(key, my_default)

But this assigns an empty string to my_value if the key is present and the value is an empty string. Is it better to use the following:

if key in dict and dict[key]:
    my_value = dict[key]
else:
    my_value = my_default

This would make use of the truthfulness of an empty string to ensure only non-empty strings were assigned to my_value.

Is there a better way to perform this check?

  • 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-17T16:54:22+00:00Added an answer on June 17, 2026 at 4:54 pm

    Maybe you mean something like:

    a.get('foo',my_default) or my_default
    

    which I think should be equivalent to the if-else conditional you have

    e.g.

    >>> a = {'foo':''}
    >>> a.get('foo','bar') or 'bar'
    'bar'
    >>> a['foo'] = 'baz'
    >>> a.get('foo','bar') or 'bar'
    'baz'
    >>> a.get('qux','bar') or 'bar'
    'bar'
    

    The advantages to this over the other version are pretty clear. This is nice because you only need to perform the lookup once and because or short circuits (As soon as it hits a True like value, it returns it. If no True-like value is found, or returns the second one).

    If your default is a function, it could be called twice if you write it as: d.get('foo',func()) or func(). In this case, you’re better off with a temporary variable to hold the return value of func.

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

Sidebar

Related Questions

Quick question on best practice. I have an AJAX application where there are 4
What's the best practice for retrieving JSON values that may not even exist in
Is there a best practice or widely accepted way of structuring and validating data
Whats the best practice for getting a commenter's email address from the cookie (this
So I have read this - https://wiki.archlinux.org/index.php/Python And it is clear from this wiki
Is there a best practice when it comes to setting client side onclick events
there are clear, clear all buttons on richfaces fileUpload component. <rich:fileUpload id=quoteFile tabindex=10 listHeight=80
There are many question regarding this topic but there is no clear answer. Although
There doesn't seem to be a clear documentation for what permissions are actually required
Is there a way to clear the contents of multiple cells, but without changing

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.