Dictionary with Lists

Mick Krippendorf mad.mick at gmx.de
Sat Oct 3 18:09:23 EDT 2009


Hi,

Shaun wrote:
> I'm trying to create a dictionary with lists as the value for each
> key.  I was looking for the most elegant way of doing it... 

from collections import defaultdict

d = defaultdict(list)

d["joe"].append("something")
d["joe"].append("another")
d["jim"].append("slow down, grasshopper")

print d


HTH,
mick.



More information about the Python-list mailing list