How can i update a dictionary with keys from a list dict_keys and values from a list dict_values? I have tried using for loop in this way:
for i in dict dict_keys:
for o in dict_values:
dict(i) = o
How can i update a dictionary with keys from a list dict_keys and values from a list dict_values? I have tried using for loop in this way:
for i in dict dict_keys:
for o in dict_values:
dict(i) = o
You can create a dictionary using a dictionary comprehension. The zip
function creates an iterator that will return a set with one item from each list, just like a zipper joins two sides of teeth. You can then combine one key and one value k:v
for each corresponding pair.
dict = {k:v for k, v in zip(dict_keys, dict_values)}
If you already have a dict
and you just want to update it with these values, simply call the update()
function on it with the same comprehension:
dict.update({k:v for k, v in zip(dict_keys, dict_values)})