Let's be concise:
keys = ['a', 'b']
values = [1, 2]
d = the_function_i_m_looking_for(keys, values)
# d = {'a': 1, 'b': 2}
Can you put a name for the_function_i_m_looking_for?
Let's be concise:
keys = ['a', 'b']
values = [1, 2]
d = the_function_i_m_looking_for(keys, values)
# d = {'a': 1, 'b': 2}
Can you put a name for the_function_i_m_looking_for?
One of many possible ways is:
{k: v for k, v in zip (keys, values) }
Another would be:
dict (zip (keys, values) )
Hence:
def the_function_you_are_looking_for (keys, values):
return dict (zip (keys, values) )
You can do this using zip to make key: value pairs and pass them into dict()
dict(zip(keys, values))
I don't know of a function that does this in one go.
This is a duplicate of this question.
The function you are looking for is zip paired with the dict constructor.
keys = ['a', 'b']
values = [1, 2]
d = dict(zip(keys, values))
print(d)
Result:
{'a': 1, 'b': 2}
For large lists you may want to use itertools.izip
If your key / value lists are different sizes izip_longest with a default value.