In this Python tutorial, we will discuss Python creates a dictionary from two lists. Here we will understand how to create a dictionary from two lists in Python.
In Python, we create a dictionary from two lists using the following 6 methods.
- Using for loop
- Using dictionary comprehension
- Using dict() & zip()
- Using OrderedDict() & zip()
- Using map()
- Using enumerate() & zip()
How to create a dictionary from two lists in python
There are multiple methods in Python to create a Python dictionary from two lists. Let us start with the first method.
Method 1: Python creates a dictionary from two lists using for loop
Here we will see how to create a dictionary from two lists using the for loop in Python.
So, in this approach, we will define 2 lists, define a dictionary, and use for loop to iterate over each element. In the last, define one list element as keys and another list element as values.
Here is an example of this approach.
# Defining two lists
keys_list = ['Name', 'Age', 'Gender', 'Country']
values_list = ['James', 32, 'Male', 'United States']
# Defining a dictionary
info_dict = {}
# Another way to defining a dictionary
# info_dict = dict()
# Using for loop to form a dictionary
for i in range(len(keys_list)):
info_dict[keys_list[i]] = values_list[i]
# Printing lists
print('Keys List:', keys_list)
print('Values List:', values_list)
# Printing dictionary
print('Dictionary:', info_dict)
In the example, we defined 2 lists keys_list and values_list respectively. After this, we defined a new empty dictionary named info_dict.
Next, we used the for loop to iterate over each value from both lists and insert them in the info_dict.
Once execution is completed, we will get the following result.
Keys List: ['Name', 'Age', 'Gender', 'Country']
Values List: ['James', 32, 'Male', 'United States']
Dictionary: {'Name': 'James', 'Age': 32, 'Gender': 'Male', 'Country': 'United States'}
Read: Python Concatenate Dictionary
Method 2: Python creates a dictionary from two lists using dictionary comprehension
Once we understand the approach of using for loop, we can also use dictionary comprehension to create a dictionary from 2 lists.
An example of using dictionary comprehension is given below.
# Defining two lists
keys_list = ['Name', 'Age', 'Gender', 'Country']
values_list = ['Scarlett', 28, 'Female', 'United States']
# Another way to defining a dictionary
# info_dict = dict()
# Using dictionary comprehension
info_dict = {keys_list[i]: values_list[i] for i in range(len(keys_list))}
# Printing lists
print('Keys List:', keys_list)
print('Values List:', values_list)
# Printing dictionary
print('Dictionary:', info_dict)
In this example, instead of defining dictionary and for loop separately, we will use dictionary comprehension. Also, when we execute the program, we will get the following result.
Keys List: ['Name', 'Age', 'Gender', 'Country']
Values List: ['Scarlett', 28, 'Female', 'United States']
Dictionary: {'Name': 'Scarlett', 'Age': 28, 'Gender': 'Female', 'Country': 'United States'}
Read: Python dictionary pop
Method 3: Python creates a dictionary from two lists using dict() and zip()
Another way to create a dictionary from two lists is by using the dict() and zip() functions.
The dict() constructor is another way to define a dictionary in Python. Moreover, this method is also used to convert a list of tuples to a dictionary.
On the other hand, the zip() function takes one or more lists, tuples, or other iterable objects. And it returns an iterator that combines the elements from these objects into tuples.
So, in this, we will pass both lists to the zip() function. This will combine the values from both lists and form a tuple. Then we will use the dict() function to convert that tuple to the dictionary.
Here is an example of this in python.
# Defining two lists
keys_list = ['Name', 'Age', 'Gender', 'Country']
values_list = ['Adam', 48, 'Male', 'United States']
# Another way to defining a dictionary
# info_dict = dict()
# Using dict() and zip()
info_dict = dict(zip(keys_list, values_list))
# Printing lists
print('Keys List:', keys_list)
print('Values List:', values_list)
# Printing dictionary
print('Dictionary:', info_dict)
In the example, we utilized the zip() and dict() functions in Python to create a dictionary using keys_list and values_list.
The result of the above example is shown below.
Keys List: ['Name', 'Age', 'Gender', 'Country']
Values List: ['Adam', 48, 'Male', 'United States']
Dictionary: {'Name': 'Adam', 'Age': 48, 'Gender': 'Male', 'Country': 'United States'}
Read: Python Dictionary index
Method 4: Python creates a dictionary from two lists using map() and lambda
Here we will create a dictionary in Python from two lists using the map() and lambda functions.
The example is shown below for using these functions in Python.
# Defining two lists
keys_list = ['Name', 'Age', 'Gender', 'Country']
values_list = ['Adam', 48, 'Male', 'United States']
# Using map() and lambda
info_dict = dict(map(lambda key, value: (key, value), keys_list, values_list))
# Printing lists
print('Keys List:', keys_list)
print('Values List:', values_list)
# Printing dictionary
print('Dictionary:', info_dict)
In the above example, we utilize the map() function which takes lambda as its first argument. Then the lambda function takes each element from keys_list and values_list respectively.
So, by using the lambda and map() functions, we are creating a tuple containing pair of each key-value pair from lists. In the last, we used the dict() function to convert that tuple to a dictionary.
Here is the final result of the above Python program.
Keys List: ['Name', 'Age', 'Gender', 'Country']
Values List: ['Adam', 48, 'Male', 'United States']
Dictionary: {'Name': 'Adam', 'Age': 48, 'Gender': 'Male', 'Country': 'United States'}
Read: Python dictionary extend
Method 5: Python creates a dictionary from two lists using OrderedDict()
Just like the dict() function, we can also use the OrderedDict() function in Python to convert the tuple to an ordered dictionary. This function returns a dictionary in the same order in which the key-value pair is inserted into it.
Let’s see an example of this in Python.
# Importing OrderedDict
from collections import OrderedDict
# Defining two lists
keys_list = ['Name', 'Age', 'Gender', 'Country']
values_list = ['Adam', 48, 'Male', 'United States']
# Using OrderedDict() and zip()
info_dict = OrderedDict(zip(keys_list, values_list))
# Printing lists
print('Keys List:', keys_list)
print('Values List:', values_list)
# Checking datatype
print('Type of Dictionary', type(info_dict))
# Printing dictionary
print('Dictionary:', info_dict)
In this example, we utilized the zip() function to create a tuple using both lists in Python. And instead of using dict(), we used the OrderedDict() function to convert a tuple to the dictionary.
Once execution is completed, we will get the following result.
Keys List: ['Name', 'Age', 'Gender', 'Country']
Values List: ['Adam', 48, 'Male', 'United States']
Type of Dictionary <class 'collections.OrderedDict'>
Dictionary: OrderedDict([('Name', 'Adam'), ('Age', 48), ('Gender', 'Male'), ('Country', 'United States')])
Read: Python Dictionary Count
Method 6: Python creates a dictionary from two lists using enumerate() & zip()
In this method, we will use the zip() function with enumerate() to create a dictionary from two lists in Python.
Here we are using the zip() and enumerate() functions together on the two Python lists to form a list of tuples. This list of tuples consists of multiple tuples of each key-value pair. And then we use the dict() constructor to convert that list of tuples to a dictionary.
Here is an example.
# Defining two lists
keys_list = ['Name', 'Age', 'Gender', 'Country']
values_list = ['Adam', 48, 'Male', 'United States']
# Creating a list of tuple using enumerate() and zip()
info_tuple = [(key, value) for x, (key, value) in enumerate(zip(keys_list, values_list))]
# Printing lists
print('Keys List:', keys_list)
print('Values List:', values_list)
# Printing tuple from 2 lists
print('Tuple:', info_tuple)
# Converting tuple to dictionary
info_dict = dict(info_tuple)
# Printing dictionary
print('Dictionary:', info_dict)
Once we execute the above Python program, we will get the following result.
You may also like to read the following python tutorials.
Conclusion
In this python tutorial, we covered 6 different methods of how to create a Python dictionary from two lists. Moreover, for each method, we have discussed an example step-by-step.
Here is the list of topics that we covered.
- Using for loop
- Using dictionary comprehension
- Using dict() & zip()
- Using OrderedDict & zip()
- Using map()
- Using enumerate() & zip()
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.