Dictionary with Lists

Shaun shaunpatterson at gmail.com
Sat Oct 3 17:59:44 EDT 2009


Hi,

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... I thought
this would work:

testDict = {}
...
testDict [1] = testDict.get (1, []).append ("Test0")  # 1 does not
exist, create empty array
print testDict
testDict [1] = testDict.get (1, []).append ("Test1")
print testDict

(Obviously I wouldn't normally write code like this.. but this is how
it would unfold in a loop)
However, the first printout gives {1: None}  instead of the desired
{1: ['test']}.  What's wrong with this syntax?

--
Shaun



More information about the Python-list mailing list