Safely add a key to a dict only if it does not already exist?

Steven D'Aprano steve+comp.lang.python at
Sat Jan 19 11:31:40 CET 2013

On Fri, 18 Jan 2013 20:15:26 -0800, Chris Rebert wrote:

> On Friday, January 18, 2013, Steven D'Aprano wrote:
>> I wish to add a key to a dict only if it doesn't already exist, but do
>> it in a thread-safe manner.
> I'm not entirely sure, but have you investigated dict.setdefault() ?

Great! That's exactly what I need, thanks to everyone who responded.


More information about the Python-list mailing list