[Tutor] Dictionary of dictionaries of dictionaries of lists.

Steve lonetwin at gmail.com
Wed Jul 7 16:15:38 CEST 2004


Hi Morten,

This is what happens when one tries to do too many things in a single
statement :)
....

> import psycopg
>
> connection = psycopg.connect("dbname=blog user=m")
> cursor = connection.cursor()
>
> cursor.execute("SELECT id, date FROM blog")
> idx = cursor.fetchall()
> entries = {}
> for i in range(len(idx)):
>     entries[idx[i][1].year] = \
>             {idx[i][1].month : {idx[i][1].day : [].append(idx[i][0])}}

when you do this:
{idx[i][1].day : [].append(idx[i][0])}

or more generally something like this:

>>> {'k' : somelist.append(blah)}

your are assigning the *return value* of the function
'somelist.append()', which is 'None', to the key 'k'.

what you could instead do is, something like:
>>> {'k' : [blah]}

or
>>> { 'k' : list(blah) }

in your case that would be:
{idx[i][1].day : [ idx[i][0] ]}
or
{idx[i][1].day : list(idx[i][0])}

HTH
Steve


More information about the Tutor mailing list