dictionary setdefault

5 views
Skip to first unread message

Sean O'Donnell

unread,
Mar 11, 2009, 10:21:43 PM3/11/09
to Python Ireland
A nice recipe using the idioms discussed tonight

http://code.activestate.com/recipes/66516/

Michael Twomey

unread,
Mar 12, 2009, 3:52:07 AM3/12/09
to python...@googlegroups.com
Also, the defaultdict class (in python 2.5 onwards) lets you specify a
factory function instead of passing in the default:

http://docs.python.org/library/collections.html#collections.defaultdict

This is particularly useful for cases where the default value takes a
little while to construct or for shaving a few more lines off your
code.

(In python 2.6 defaultdict is right beside my new favourite class,
namedtuple: http://docs.python.org/library/collections.html#collections.namedtuple)

mick

Tim Kersten

unread,
Mar 12, 2009, 4:34:07 AM3/12/09
to python...@googlegroups.com
Sweeeeet namedtuples! :-) I like.
Reply all
Reply to author
Forward
0 new messages