Categories
Python Answers

How to convert two lists into a dictionary with Python?

Spread the love

Sometimes, we want to convert two lists into a dictionary with Python.

In this article, we’ll look at how to convert two lists into a dictionary with Python.

How to convert two lists into a dictionary with Python?

To convert two lists into a dictionary with Python, we can use the dict and zip functions.

For instance, we write:

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

We call zip with the keys and values to combine the entries of each into tuples.

Then we call dict to turn each tuple into an entry in the dictionary.

Therefore, dictionary is {'a': 1, 'b': 2, 'c': 3}.

Conclusion

To convert two lists into a dictionary with Python, we can use the dict and zip functions.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *