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

Vito De Tullio vito.detullio at gmail.com
Sat Jan 19 03:19:03 EST 2013


Peter Otten wrote:

uhhmm.. I think I misread the example

>>>> d = {}
>>>> d.setdefault(1, 2)
> 2
>>>> d
> {1: 2}
>>>> d.setdefault(1, 3)
> 2
>>>> d
> {1: 2}

yeah, sure it can be useful for the OP...

-- 
ZeD




More information about the Python-list mailing list