Use dict.setdefault():
>>> d = {1: 'one'}
>>> d.setdefault(1, '1')
'one'
>>> d # d has not changed because the key already existed
{1: 'one'}
>>> d.setdefault(2, 'two')
'two'
>>> d
{1: 'one', 2: 'two'}from : https://stackoverflow.com/questions/42315072/python-update-a-key-in-dict-if-it-doesnt-exist
No comments:
Post a Comment