0

I am trying to construct a dictionary with a list of IDs, which will be used as the response from an API. The dict will be used to generate a JSON response.

I am trying to append a list of IDs that have been affected by the request, but am experiencing an error shown below the code.

I have the following code:

data = {"data" : { "message" : "UPDATED TICKETS SUCCESSFULLY", "status" : 200}}
data['data'].append({"child_ids" : child_sr_ids})

The error that I am getting is: AttributeError: 'dict' object has no attribute 'append'

This seems to contradict other sources that I have read such as this: Appending to list in Python dictionary

I am sure I'm doing something simple wrong but I can't work it out. Would appreciate some direction.

edit: title has been changed to better reflect the correct terminology

btongeorge
  • 421
  • 2
  • 12
  • 23

3 Answers3

1

You're not appending to a list, you're adding a key to the inner dictionary. What you're looking for is:

data['data']['child_ids'] = child_sr_ids
Bahrom
  • 4,752
  • 32
  • 41
1

data["data"] is not a list. It is a dictionary. There isn't a single list in your code. I think what you want to do here is:

data["data"]["child_ids"] = child_sr_ids

Qwerty
  • 1,252
  • 1
  • 11
  • 23
0

simply try the line below or use dict update

data["new key"] = "some new entry" 
OBu
  • 4,977
  • 3
  • 29
  • 45
Raj
  • 462
  • 3
  • 15