How do I convert two lists into a dictionary?

I want to combine these:

keys = ['name', 'age', 'food']
values = ['Monty', 42, 'spam']

Into a single dictionary:

{'name': 'Monty', 'age': 42, 'food': 'spam'}

1
18

Use zip to create a list of (key, value) tuples, then apply the dict constructor:

keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
print(dictionary)

Output:

{'a': 1, 'b': 2, 'c': 3}

Leave a Comment