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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:40:35+00:00 2026-06-13T10:40:35+00:00

def function(varone=None, vartwo=None, varthree=None): values = {} if var1 is not None: values[‘var1’] =

  • 0
def function(varone=None, vartwo=None, varthree=None):
     values = {}
            if var1 is not None:   
                    values['var1'] = varone
            if var2 is not None:
                    values['var2'] = vartwo
            if var3 is not None:
                    values['var3'] = varthree
            if not values:
                    raise Exception("No values provided")

Can someone suggest a more elegant, pythonic way to accomplish taking placing non-null named variables and placing them in a dictionary? I do not want the values to be passed in as a dictionary. The key names of “values” are important and must be as they are. The value of “varone” must go into var1, “vartwo” must go into var2 and so on; Thanks.

  • 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-13T10:40:36+00:00Added an answer on June 13, 2026 at 10:40 am

    You could use kwargs:

    def function(*args, **kwargs):
        values = {}
        for k in kwargs:
            if kwargs[k] is not None:
                values[k] = kwargs[k]
        if not values:
            raise Exception("No values provided")
        return values
    
    >>> function(varone=None, vartwo="fish", varthree=None)
    {'vartwo': 'fish'}
    

    With this syntax, Python removes the need to explicitly specify any argument list, and allows functions to handle any old keyword arguments they want.

    If you’re specifically looking for keys var1 etc instead of varone you just modify the function call:

    >>> function(var1=None, var2="fish", var3=None)
    {'var2': 'fish'}
    

    If you want to be REALLY slick, you can use list comprehensions:

    def function(**kwargs):
        values = dict([i for i in kwargs.iteritems() if i[1] != None])
        if not values:
            raise Exception("foo")
        return values
    

    Again, you’ll have to alter your parameter names to be consistent with your output keys.

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

Sidebar

Related Questions

consider this simple function def foo(l=[]): if not l: print List is empty else
level: beginner the following code will print 'False' def function(x): if len(x) == 5:
Given the following function: def foo(a, b, c): pass How would one obtain a
I have a function: def create(sender, **kw): [...] Which should be called when the
I'm using this simple function: def print_players(players): tot = 1 for p in players:
Below is my RUN_SQL function: def RUN_SQL_SAFE(sql, input_tuple=(), get_update_id=False, debug = False): conn =
In python I have the following function: def is_a_nice_element(element, parameter): #do something return True
I have a function foo that accepts a Boolean function def foo( f:(_)=>Boolean )
Here is the code for the relevant function: def populateSubscribers(csvfile, db): conn = sqlite3.connect(db)
I have the following pattern matching case in a scala function: def someFunction(sequences: Iterable[Seq[Int]]):Seq[Int]

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.