Only add to a dict if a condition is met

You’ll have to add the key separately, after the creating the initial dict:

params = {'apple': apple}
if orange is not None:
    params['orange'] = orange
params = urllib.urlencode(params)

Python has no syntax to define a key as conditional; you could use a dict comprehension if you already had everything in a sequence:

params = urllib.urlencode({k: v for k, v in (('orange', orange), ('apple', apple)) if v is not None})

but that’s not very readable.

If you are using Python 3.9 or newer, you could use the new dict merging operator support and a conditional expression:

params = urllib.urlencode(
    {'apple': apple} | 
    ({'orange': orange} if orange is not None else {})
)

but I find readability suffers, and so would probably still use a separate if expression:

params = {'apple': apple}
if orange is not None:
    params |= {'orange': orange}
params = urllib.urlencode(params)

Another option is to use dictionary unpacking, but for a single key that’s not all that more readable:

params = urllib.urlencode({
    'apple': apple,
    **({'orange': orange} if orange is not None else {})
})

I personally would never use this, it’s too hacky and is not nearly as explicit and clear as using a separate if statement. As the Zen of Python states: Readability counts.

Leave a Comment