I want to combine these:
JavaScript
x
3
1
keys = ['name', 'age', 'food']
2
values = ['Monty', 42, 'spam']
3
Into a single dictionary:
JavaScript
1
2
1
{'name': 'Monty', 'age': 42, 'food': 'spam'}
2
Advertisement
Answer
Like this:
JavaScript
1
5
1
keys = ['a', 'b', 'c']
2
values = [1, 2, 3]
3
dictionary = dict(zip(keys, values))
4
print(dictionary) # {'a': 1, 'b': 2, 'c': 3}
5
Voila :-) The pairwise dict
constructor and zip
function are awesomely useful.