> d.setdefault(key, value) I though the OP wanted an error if the key already existed.
This is close, as it won’t change the dict if the key is already there, but it will add it if it’s not. @OP Maybe post those five lines so we know exactly what you want — maybe there is already a good solution. I know I spent years thinking “there should be an easy way to do this” before I found setdefault(). -CHB > > :-) > > [snip] > _______________________________________________ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/