1

I am trying to append an object in a loop to one of two sub keys created in a json object.

for agency in q:
    dashboard[agency.id]= []
    dashboard[agency.id].append({"name": agency.agency.name})
    dashboard[agency.id].append({"stats": []})
    dashboard[agency.id].append({"users": []})

for row in stats:
    if row['is_agency']:
        dashboard[row['agency_id']['stats']].append(dict(row))
    else:
        dashboard[row['agency_id']['users']].append(dict(row))

But it is throwing an error of:

dashboard[row['agency_id']]['users'].append(dict(row))
TypeError: list indices must be integers, not str

If I remove ['users'] or ['stats'] key it appends to the agency.id key just fine. But when I try to add it as a second level key, it throws the above error.

As the json object will always have the 3 sub keys (name[0], stats[1], users[2]) I have also then tried using:

for row in stats:
    if row['is_agency']:
        dashboard[row['agency_id']][1].append(dict(row))
    else:
        dashboard[row['agency_id']][2].append(dict(row))

That results in an error of the following:

dashboard[row['agency_id']][2].append(dict(row))
AttributeError: 'dict' object has no attribute 'append'
5
  • Is row a dictionary? Commented May 28, 2017 at 18:03
  • No its a python object from: stats = db.engine.execute(text("CALL dashboard("+str(cu.id)+", '"+cu.user_type+"', "+str(cu.id)+", '2017-05-01', '2017-05-30');")).fetchall(). Calling any row['column'] works fine as displayed using row['agency_id'] in the if statement., I also convert it to a dictionary using dict() when I append it to the proper place in the json. (which also works if I append it to the main node of the json object by agency_id). Only issue I have is with syntax trying to find the sub key of 'stats' or 'user' Commented May 28, 2017 at 18:05
  • Moses: you have pasted back the same thing I typed. I have also tried moving the second node into the bracket as such: dashboard[row['agency_id']['stats']].append() without success. Commented May 28, 2017 at 18:08
  • dashboard[row['agency_id']][1]['stats'].append(row) and dashboard[row['agency_id']][2]['users'].append(row) Commented May 28, 2017 at 18:18
  • Moses: While this seems to be working (need to do more research) I am not sure I understand the solution. Why do you need the object index and the key name? Seems like you would always just use one or the other. At minimum just the key index. Regardless thank you for your help! Commented May 28, 2017 at 18:27

1 Answer 1

1

You are confusing the types and methods of your objects, as user @Moses Koledoye shows, if you do:

dashboard[row['agency_id']][1]['stats'].append(row)

Step by Step:

row['agency_id'] is an Int, so

dashboard[row['agency_id']] #has type list

will give you the value of the key row['agency_id'], of type List

dashboard[row['agency_id']][1] 

gives to you the value in the position 1 of the list

`dashboard[row['agency_id']][1]['stats']` #is a List also

finally gives to you the list of stats (taking it as example), that's why you can use the append method:

dashboard[row['agency_id']][1]['stats'].append(row)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the explanation. Makes perfect sense! And Moses for the solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.