Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (12.7k points)

Let's say, I have:

keys = ['name', 'age', 'food']

values = ['Monty', 42, 'spam']

I want to change them into this:

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

Is there any simple way to do this?

1 Answer

0 votes
by (26.4k points)

Try this following code:

>>> keys = ['a', 'b', 'c']

>>> values = [1, 2, 3]

>>> dictionary = dict(zip(keys, values))

>>> print(dictionary)

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

Want to learn more about python? Come and join: python online course

Related questions

Browse Categories

...