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

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jan 19 05:31:40 EST 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.



-- 
Steven



More information about the Python-list mailing list