pythondictionarykeyword-argument

Converting Python dict to kwargs?


I want to build a query for sunburnt (solr interface) using class inheritance and therefore adding key-value pairs together. The sunburnt interface takes keyword arguments. How can I transform a dict ({'type':'Event'}) into keyword arguments (type='Event')?


See also: What do ** (double star/asterisk) and * (star/asterisk) mean in a function call? - the corresponding question for people who encounter the syntax and are confused by it.


Solution

  • Use the double-star (aka double-splat?) operator:

    func(**{'type':'Event'})
    

    is equivalent to

    func(type='Event')