Home > OS >  Update dictionary with key value pair in for loop
Update dictionary with key value pair in for loop

Time:09-20

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?

CodePudding user response:

You can do this:

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