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.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1431

Leave a Reply