1

I am new to python and have a perhaps basic question.. I have a dictionary, and want to add another dictionary to a key in the first dictionary :)

So currently the dictionary look like this:

{
    "name": "custname",
    "slug": "custslug",
    "group": "1"
}

And I need it to "append" another dictionary to an existing.. I guess It is a nested dictionary I need.

{
    "name": "custname",
    "slug": "custslug",
    "custom_fields": {
        "NavID": "10023"
    },
    "group": "1",
 }
Mark
  • 90,562
  • 7
  • 108
  • 148
  • 3
    `dict[key] = other_dict` – rdas Jun 20 '20 at 16:54
  • Do you mean https://stackoverflow.com/questions/1024847/how-can-i-add-new-keys-to-a-dictionary or https://stackoverflow.com/questions/38987/how-do-i-merge-two-dictionaries-in-a-single-expression-in-python? – mercator Jun 20 '20 at 17:04

4 Answers4

3

In case you want a nested dictionary use the below method -

d1 = {"name": "custname","slug": "custslug","group": "1"}
d1["custom_fields"] = {"NavID": "10023"}
print(d1)

This will give the output you wanted.

Or if you want to merge two dictionaries and update the values, you may use the update method.

d1 = {"name": "custname","slug": "custslug","group": "1"}
d2 = {"NavID": "10023"}
d1.update(d2)

The output here will be

{"name": "custname","slug": "custslug","group": "1","NavID": "10023"}
Ayush Jain
  • 154
  • 1
  • 7
  • Second dictionary is not a nested dictionary. This is not what OP meant. – Prashant Kumar Jun 20 '20 at 17:07
  • But when I convert my data frame to a dict with this line new_tenants = nav_kunder.to_dict(orient='records') I then get a TypeError: list indices must be integers or slices, not str when I use your first suggestion – Mathias Moesgaard Jun 20 '20 at 17:10
  • 1
    I was also confused with what the question asked.. So I came up with both update method and assigning the second dictionary to a key of the first dictionary and thus making it a nested dictionary. – Ayush Jain Jun 20 '20 at 17:13
2
dict = {
    "name": "custname",
    "slug": "custslug",
    "group": "1"
}

another_dict = {
        "NavID": "10023"
    }

dict['Custom_fields'] = another_dict
Prashant Kumar
  • 2,057
  • 2
  • 9
  • 22
Rohit S K
  • 1,178
  • 3
  • 13
0

Great question! This will work.

d1 = {
    "name": "custname",
    "slug": "custslug",
    "group": "1"
}
d2 = {"NavID": "10023"}
d1["custom_fields"] = d2
JimmyCarlos
  • 1,934
  • 1
  • 10
  • 24
  • This is not what OP meant in his question I think. He says the second dictionary is a nested dictionary. You are adding a dictionary explicitly to a key. I think you should clarify once. – Prashant Kumar Jun 20 '20 at 17:03
0

If you want to append or merge 2 dictionaries, you can use update method.

Below is how you can do it :

d1 = {
    "name": "custname",
    "slug": "custslug",
    "group": "1"
}
d2 = {"custom_fields": {"NavID": "10023"}}


d1.update(d2)

print(d1)

OUTPUT :

>>> {'group': '1', 'name': 'custname', 'custom_fields': {'NavID': '10023'}, 'slug': 'custslug'}
Prashant Kumar
  • 2,057
  • 2
  • 9
  • 22