How to Convert Two Lists Into Dictionary in Python

Last Updated on June 26, 2021 by Roshan Parihar

In this tutorial, learn how to convert two Lists into Dictionary in Python. The short answer is to use the dict() function inside which you have to use the zip() function.

You can also use the for loop to create a Dictionary from two Lists using Python. Let’s find out the different useful examples given below here.

Method 1: Using dict() with zip() to Convert Two Lists Into Dictionary in Python

To convert the two Lists into Dictionary, you have to use the dict(). Inside that function, you have to use the zip() as pass the comma-separated two Lists variables as its arguments. The result gives the converted two Lists into Dictionary.

Output

Dictionary after conversion is: {‘Billy’: 11, ‘John’: 23.9, ‘Rock’: 19}

The above example shows the output in which the first List becomes keys and the second List becomes values.

Method 2: Using For Loop to Change Two Lists into Dictionary in Python

You can change the two Lists into Dictionary using the for loop of Python. Firstly, you have to define a blank Dictionary variable in Python. After that, use the for loop and store the keys and values to Dictionary in each iteration.

Output

Dictionary after conversion is: {‘Billy’: 11, ‘John’: 23.9, ‘Rock’: 19}

The output contains the same Dictionary variable and its elements that you get in the first example above. It takes some time to run the loop and add the items to Dictionary in each iteration.

Method 3: Using Dictionary Comprehension in Python

In addition to the above methods, you can also use Dictionary comprehension to create Dictionary from two Lists variables. See the example given below to learn the method of using the Dictionary comprehension using Python.

Output

Dictionary after conversion is: {‘Billy’: 11, ‘John’: 23.9, ‘Rock’: 19}

When you execute the above code, you will also get the same result as the first and second examples above.

You May Also Like to Read

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.