Union Operators to Dict

How can I convert

x = 'a=b&c=d'

to

{'a':'b', 'c':'d'}

without explicitly looking and replacing & and = to , and : respectively?

Answer

You have a DSL (domain specific language) for defining a dict. You have to parse the string, then evaluate the result. Luckily, that’s as simple as

d = dict(kv.split("=", 1) for kv in x.split("&"))