Here I get a key error even I check if the key exists in the dict :
if (('element' in d.keys()) & (d['element'] == 1)):
The expression x and y first evaluates x; if x is false, its value is
returned; otherwise, y is evaluated and the resulting value is
& is "bitwise and",
and is the logical "and" operator, they are not the same thing.
You should use
and, and you can also remove the unneeded parenthesis for readability.
You also don't even have to call the
if 'element' in d and d['element'] == 1: