简体   繁体   中英

update a nested dictionary with values of a list

Need to iterate the list values in the nested dictionary

d = { 'a' :{'a': '3','b': '2 '},'b':{'c':'1'}}

temp = (20,31,111,455,55,6)

for i in d:
    for j in d[i]:
        for k in temp:
            d[i][j]=k

print d

I expect the following:

d = { 'a' :{'a': '20','b': '31 '},'b':{'c':'111'}}

Try this:

d = { 'a' :{'a': '3','b': '2 '},'b':{'c':'1'}}

temp = (20,31,111,455,55,6)
count=0
for i in d:
    for j in d[i]:
        #update nested dictionary value
        d[i][j]=temp[count]
        #increment count variable
        count+=1

print(d)

O/P:

{'a': {'a': 20, 'b': 31}, 'b': {'c': 111}}

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