Ham*_*ait 1 python dictionary list
I have a dictionary I want to update:
my_dict = {"a":"A", "b":"B"}
Run Code Online (Sandbox Code Playgroud)
and a list with the exact same length:
my_list = ["D", "E"]
Run Code Online (Sandbox Code Playgroud)
I would like to find the most efficient way to update my_dict with the values from my_list to:
{"a":"D", "b":"E"}
Run Code Online (Sandbox Code Playgroud)
without having to run multiple for loops or similar. I tried to do this with list comprehensions, but it does not allow multiple statements:
{my_dict_key:list_item for my_dict_key in my_dict.keys(), list_item in my_list}
Run Code Online (Sandbox Code Playgroud)
Use zip() with a dictionary-comprehension:
{key: value for key, value in zip(my_dict, my_list)}
Run Code Online (Sandbox Code Playgroud)
Or just:
dict(zip(my_dict, my_list))
Run Code Online (Sandbox Code Playgroud)