Preferred Python idiom for handling non-existing dictionary keys and why?

Gerrit Holl gerrit at nl.linux.org
Fri Oct 10 15:59:05 EDT 2003


Skip Montanaro wrote:
>     >> Say I am populating a dictionary with a list and appending. I have
>     >> written it thusly:
> 
>     John> You want the dict.setdefault method.
> 
> d.setdefault() never made any sense to me (IOW, to use it I always had to
> look it up).

I have had a wrong idea about setdefault for a very long time. To me, it
sounds like: "set this value the default value of dict, so after this
call, let each non-existing key result in this value".

Gerrit.

-- 
261. If any one hire a herdsman for cattle or sheep, he shall pay him
eight gur of corn per annum.
        -- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Kom in verzet tegen dit kabinet:
	http://www.sp.nl/





More information about the Python-list mailing list