Skip to content Skip to sidebar Skip to footer

Creating A Nested Dictionary Comprehension In Python 2.7

I have a nested tuple returned from a MySQL cursor.fetchall() containing some results in the form (datetime.date, float). I need to separate these out in to a nested dictionary of

Solution 1:

it seems difficult to do both levels in a concise oneliner, I propose you instead to use defaultdict like this:

res = defaultdict(dict)
for z in raw:
    res["%02d/%04d"%(z[0].month, z[0].year)][z[0].day] = z

Post a Comment for "Creating A Nested Dictionary Comprehension In Python 2.7"