This post will discuss how to build a dictionary from a list of keys and values in Python.

For example, keys = ['A', 'B', 'C'] and values = [1, 2, 3] should result in the dictionary {'A': 1, 'B': 2, 'C': 3}.

1. Using zip() with dict() function

The simplest and most elegant way to build a dictionary from a list of keys and values is to use the zip() function with a dictionary constructor.

Download  Run Code

2. Using Dictionary Comprehension

Another approach is to use the zip() with dictionary comprehension. This is often useful when you need value-key pairs instead of key-value pairs or apply some mapping function to each key or value. This can be implemented as:

Download  Run Code

 
Here’s what the code would look like without zip():

Download  Run Code

 
If you need to create a new dictionary with keys from iterable with the same value, you can do the following.

Download  Run Code

 
Alternatively, you can use the fromkeys() class function:

Download  Run Code

That’s all about building a dictionary from a list of keys and values in Python.