You do not need to call d.keys()
, so
if key not in d:
d[key] = value
is enough. There is no clearer, more readable method.
You could update again with dict.get()
, which would return an existing value if the key is already present:
d[key] = d.get(key, value)
but I strongly recommend against this; this is code golfing, hindering maintenance and readability.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…