use dict.get() to increase value

in note-timknip •  4 years ago 
a_dict = {"a":1}

a_dict["b"] = a_dict.get("b", 0) + 1

print(a_dict)
> {'a': 1, 'b': 1}

a_dict["a"] = a_dict.get("a", 0) + 1

print(a_dict)
> {'a': 2, 'b': 1}
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!