简体   繁体   中英

How to update values in a dictionary using value parameters(not using keys) in python?

I have a dictionary value as follows;(For example key= keyA)

{'state': 'unprocessed', 'date_populated': '2017-09-26 02:07:58.535131', 'site': 'Site1'}

I want to update the ' state ' parameter in the value to ' processed '.

How can I do that?

I know I can update like

cacheDictionary.update({keyA:'newValue'})

But in the above approach I will miss my timestamps. I need to keep that timestamps and only update state parameter

Reassign

Just reassign it. dictionary["state"] = "processed" .

dictionary.update(insert) will add all of the key: value pairs in insert , which is not what you want.

You can also do dictionary.update({"state": "processed"}) , which overwrites it.

Essentially, dictionary.update(insert) is equivalent to saying for key in insert.keys(): dictionary[key] = insert[key]

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