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
Answers:
Use the following code.
output = {name: complete_dict[name] for name in new_result}
Have you tried:
output = dict((name, complete_dict[name]) for name in new_result)
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
Use the following code.
output = {name: complete_dict[name] for name in new_result}
Have you tried:
output = dict((name, complete_dict[name]) for name in new_result)