list - Iteration Through tuple of dictionaries in Python -


i trying iterate through tuple of dictionaries using python, value i'm looking , modify dictionary value. example:

dict = {'1': 'one', '2': 'three'}  tuple = ({'1': 'one', '5': 'five'}, {'4': 'four', '2': 'two'}) 

my goal modify dict , replace 'three' 'two' second dictionary in tuple.

i know how iterate through dictionaries using loops , dict.items(), can't seem tuple...

any appreciated!

just check each dict d key , set dict["2"] equal d["2"].

dict = {'1': 'one', '2': 'three'}  tuple = ({'1': 'one', '5': 'five'}, {'4': 'four', '2': 'two'})  d in tuple:     if "2" in d:         dict["2"] = d["2"] 

if have multiple dicts in tuple have same key value set last dict encounter. if wanted first match should break in if.

dict = {'1': 'one', '2': 'three'}  tuple = ({'1': 'one', '5': 'five'}, {'4': 'four', '2': 'two'}) d in tuple:     if "2" in d:         dict["2"] = d["2"]         break # first match 

if want last match better start @ end of tuple:

for d in reversed(tuple):     if "2" in d:         dict["2"] = d["2"]         break # last dict in tuple has key 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -