json - Searching for and updating a dictionary in a list of dictionaries in python -


so know this: python list of dictionaries search

answers first part of question.

however, issue such: want update searched dictionary directly, not retrieve it.

more specifically, have list of dictionaries. dictionaries each have "list" key value list. want update list once find dictionary.

the structure each dictionary such: {"name": "whatever", "display":"whoever", "list": [x, y, z]}

so example list be:

[     {"name": "whatever", "display":"whoever", "list": [x, y, z]},     {"name": "whatever2", "display":"whoever2", "list": [x2, y2, z2]},     {"name": "whatever3", "display":"whoever3", "list": [x3, y3, z3]} ] 

i want retrieve, say, dictionary name "whatever2" , add a2 "list".

what best way this?

"best" can weigh "python-ness" / "performance" / code clarity etc.

l = [     {"name": "whatever", "display":"whoever", "list": [x, y, z]},     {"name": "whatever2", "display":"whoever2", "list": [x2, y2, z2]},     {"name": "whatever3", "display":"whoever3", "list": [x3, y3, z3]} ]  d in l:     if d['name'] != 'whatever2': continue     d['list'].append('a2')     break  # unless have multiple dictionaries in list same name 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -