edward serfontein
edward serfontein

Reputation: 3

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?

Upvotes: 0

Views: 672

Answers (1)

Animesh Singh
Animesh Singh

Reputation: 61

You can do this:

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

Upvotes: 2

Related Questions