Create a dictionary by taking key and value from a loop

Question:

I would like to create a dictionary called output, taking key and value from a loop. I would like to create a dictionary that has name as key, and complete_dict[name] as value.

So key: name, value: complete_dict[name]

output = {}

for name in new_result:
    x = (name, complete_dict[name])

How can I create it? Thank you

Asked By: Takao貴男

||

Answers:

Use the following code.

output = {name: complete_dict[name] for name in new_result}
Answered By: Gilseung Ahn

Have you tried:

output = dict((name, complete_dict[name]) for name in new_result)
Answered By: yeaske