Question Details

No question body available.

Tags

python postgresql dictionary

Answers (1)

April 17, 2026 Score: 1 Rep: 429 Quality: Low Completeness: 50%

It looks like you are overwriting outerdict[table] on every row iteration.

You are overwriting outerdict[table] for every iteration. I would define a list to append all the dictionaries to and then append it to that.

I recommend using the index as a key instead:

outerdict[table] = {}
for i, row in enumerate(rows):
    name, department, overallpercentage = row
    outerdict[table][i] = {
        'name': name,
        'department': department,
        'overallpercentage': overallpercentage
    }

EDIT: I also want to point out that the structure you want isn't valid in Python, sets cannot contain dictionaries. With the above, your output would look like this:

{'ClassA': {0: {...}, 1: {...}, 2: {...}}}