Insert missing keys using defaultdict

George Sakkis george.sakkis at
Thu Mar 11 17:20:47 CET 2010

On Mar 11, 5:02 pm, Gnarlodious <gnarlodi... at> wrote:

> I am trying to grok this documentation but need help:
> In a perfect world the dict looks like this:
> plistDict={'Style':'ExternalURL', 'Ref':'',
> 'Tip':'Opens in a new window', 'Text':''}
> Let's say I want to prep a dict from a plist to insert the values into
> an HTML link string:
> "<a class='%(Style)s' href='%(Ref)s' title='%(Tip)s'>%(Text)s</a>" %
> plistDict
> However, in this imperfect world the dict might look like this:
> plistDict={'Ref':'', 'Text':''}
> which would error:
> KeyError: 'Style'
> So using defaultdict:
> from collections import defaultdict
> How do create a dict assigning every missing key with a default
> string?

"<a class='%(Style)s' href='%(Ref)s' title='%(Tip)s'>%(Text)s</a>" %
defaultdict(lambda:'_MISSING_', plistDict)


More information about the Python-list mailing list