[Tutor] dictionary append

Kent Johnson kent37 at tds.net
Fri Nov 2 00:43:41 CET 2007

bob gailer wrote:
>     if key not in keywords:
>        keywords[key] = []
>     keywords[key].append(value)

This can be written more succinctly as
   keywords.setdefault(key, []).append(value)

or in Python 2.5:
from collections import defaultdict
keywords = defaultdict(list)


More information about the Tutor mailing list