简体   繁体   中英

Update dictionary with key value pair in for loop

I have a dictionary dict1 . I want to iterate through the dict and add the output as key-value pairs in another dict2

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

dict2={}

for i in dict1:
   if i meets condition:
      add it to dict 2.

How would I right the last line in the loop?

You can do this:

for key, value in dict1.items():
      dict2[key] = value

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