Most pythonic way of assigning keyword arguments using a variable as keyword?

Solution 1:

Use keyword argument unpacking:

>>> kw = {'a': True}

>>> f(**kw)
<<< 'a was True'

Solution 2:

In many circumstances you can just use

f(kw)

as keyword arguments don't have to be specified as keywords, if you specify all arguments before them.

Python 3 has a syntax for keyword only arguments, but that's not what they are by default.

Or, building on @zeekay's answer,

kw = 'a'
f(**{kw: True})

if you don't want to store kw as a dict, for example if you're also using it as a key in a dictionary lookup elsewhere.