简体   繁体   中英

Update a list in a nested dictionary

I have a nested dicts. Here is my dict:

org_query = {"query": {"bool": {"must": [],"must_not": []}}}

I want to update another dict inside the nested dict. Here's the dict i want to add:

query_form =  { "match_phrase": { "name": "steve" }}

Required Output:

org_query = {"query": {"bool": {"must": [{ "match_phrase": { "name": "steve" }],"must_not": []}}}

I found this Update value of a nested dictionary of varying depth but it updates the value. In my case, i want to update the entire dict to the list in the key of the nested dict. How to make it possible.

In your case, that would be:

org_query["query"]["bool"]["must"].append(query_form)

More here: https://www.w3schools.com/python/python_dictionaries.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM