Shorter way to write list-as-dict-value in Python? I have a data structure that looks like this:

a = {
     'red': ['yellow', 'green', 'purple'],
     'orange': ['fuschia']
    }


This is the code I write to add new elements:

if a.has_key(color):
    a[color].append(path)
else:
    a[color] = [path]


Is there a shorter way to write this?
You can use collections.defaultdict:

from collections import defaultdict

mydict = defaultdict(list)


Now you just need:

mydict[color].append(path)


defaultdict is a subclass of dict that can be initialized to a list, integer...    



btw, the use of has_key is discouraged, and in fact has_key has been removed from  python 3.2.
When needed, use this by far more pythonic idiom instead:

if color in a:
    ........