Convert Two Lists into Dictionary in Python

A list is a collection of items provided by the Python programming language, whereas a dictionary in Python is used to store data into key and value pairs. If you want to learn about converting two Python lists into a dictionary, this article is for you. In this article, I will take you through a tutorial on how to convert two lists into a dictionary in Python.

Convert Two Lists into Dictionary in Python

A list is a collection of data where we can store data of any data type. So, for converting two lists into a dictionary, we first need two lists that are related to each other in such a way that we can create key and value pairs.

So first, let’s create two Python lists based on the data of an employee:

keys = ["Employee Name", "Employee Role", "Department"]
values = ["John", "SEO Manager", "Marketing"]

In the above code, I have created two lists as keys and values. The keys contain the heads of employee data, and the values contain the data of an employee. Now here’s how we can convert two Python lists into a dictionary:

info = dict(zip(keys, values))
print(info)
{'Employee Name': 'John', 'Employee Role': 'SEO Manager', 'Department': 'Marketing'}

In the above code, I have used the zip function in Python inside the dict() method that stores both the lists (keys and values) into a dictionary known as info.

Summary

So this is how to convert two Python lists into a dictionary. A list is a collection of items provided by the Python programming language, whereas a dictionary in Python is used to store data into key and value pairs. I hope you liked this article on converting two Python lists into a dictionary. Feel free to ask valuable questions in the comments section below.

Default image
Aman Kharwal

Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder

Articles: 1211

Leave a Reply